Android AppBarLayout背景
我想这样做 在Android Studio的预览中看起来不错,但在运行时我得到了这个 正如你们在屏幕开始时看到的,颜色是白色的,我想用我自己的颜色,在这个例子中是绿色的 最初使用的是Cordinator布局,但我需要使用抽屉布局用于菜单 这里是我的XMLAndroid AppBarLayout背景,android,android-5.0-lollipop,material-design,Android,Android 5.0 Lollipop,Material Design,我想这样做 在Android Studio的预览中看起来不错,但在运行时我得到了这个 正如你们在屏幕开始时看到的,颜色是白色的,我想用我自己的颜色,在这个例子中是绿色的 最初使用的是Cordinator布局,但我需要使用抽屉布局用于菜单 这里是我的XML <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id=
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true"
android:background="@drawable/bg_app"
tools:context=".ui.home.HomeActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/green_tataki"
app:popupTheme="@style/AppTheme.PopupOverlay">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_user"
android:background="@color/green_tataki"
android:layout_marginRight="5dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/menu_icon"
android:padding="20dp"
android:src="@drawable/ic_menu"
android:background="@color/green_tataki"/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@color/white"
android:layout_marginTop="?attr/actionBarSize"
android:layout_gravity="right"
android:choiceMode="singleChoice"
android:dividerHeight="1dp"
android:background="@color/brown_tataki"
android:id="@+id/left_drawer"/>
</android.support.v4.widget.DrawerLayout>
这是代码
public class HomeActivity extends AppCompatActivity {
public static final String TAG = "HomeActivity";
//==============Menu Variables=====================
private ListView listViewItems;
private ArrayList<MenuBean> arrayOptionMenu;
private DrawerLayout mDrawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
ImageButton iconMenu = (ImageButton) findViewById(R.id.menu_icon);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.END)) {
drawer.closeDrawer(GravityCompat.END);
} else {
super.onBackPressed();
}
}
}
公共类HomeActivity扩展了AppCompatActivity{
公共静态最终字符串TAG=“HomeActivity”;
//================菜单变量=====================
私有ListView listViewItems;
private ArrayList arrayOptionMenu;
私人抽屉布局mDrawerLayout;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
getSupportActionBar().setDisplayShowTitleEnabled(false);
ImageButton图标菜单=(ImageButton)findViewById(R.id.menu_图标);
}
@凌驾
public void onBackPressed(){
抽屉布局抽屉=(抽屉布局)findViewById(R.id.抽屉布局);
if(抽屉isDrawerOpen(重力比较结束)){
抽屉。闭式抽屉(重力compat.END);
}否则{
super.onBackPressed();
}
}
}
状态栏着色的典型方法是在主题上设置colorPrimaryDark
属性:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimaryDark">@color/green_tataki</item>
</style>
@颜色/绿色\u tataki
另外,我建议仍然使用CoordinatorLayout。在您提供的代码中,您可以使用Coordinator布局替换RelativeLayout 关于AppBar和StatusBar的着色以及它们如何与CoordinatorLayout一起工作,有很多令人困惑的信息。作为一项规则,我建议尽可能保持事情的简单性,而不是尝试将多个解决方案混为一谈 在您的情况下,您应该像最初一样使用坐标布局替换RelativeLayout 如上所述,状态栏的默认颜色将是主主题中的colorPrimaryDark。推荐使用此颜色的是免费提供的较深版本的colorPrimary(用于工具栏) 在v21/styles.xml中,使用以下设置:
<style name="Theme.MyApp" parent="Base.Theme.MyApp">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
从协调人那里。这将阻止布局填充整个视图空间(它应该在状态栏之后启动)。如果下面没有内容,状态栏应该默认为colorPrimaryDark
所有这些在模拟器上看起来都很好,但在真实设备上却不好的原因可能是因为您正在测试的真实设备低于v21。在此之前,无法为状态栏着色
由于设计库早期版本中存在一些缺陷,我还建议您使用最新的版本,在撰写本文时,该版本是“com.android.support:design:23.3.0”
有关更多详细信息,请查看github上Chris Banes的示例应用程序:。此示例中的主要活动似乎与您自己的要求非常接近。我认为您添加了所有要设置文件样式的颜色,这样android studio就显示了正确的主题,但是当您在任何设备上运行应用程序时,通知栏不会改变,如果您的情况如此,那么解决方案非常简单。 检查app gradle文件上的targetSdkVersion值
如果值<21,则应将其更改为21我有您指定的样式文件,因此在IDE预览中效果良好。您可能还需要使用
setStatusBarBackgroundColor
方法手动设置抽屉布局上的状态栏背景色。这里的回答很好地说明了这一点:这应该是一个评论。也许你是对的,但我认为他在styles文件中制作了所有内容,所以android studio显示了他想要的主题,但是当在任何设备上运行应用程序时,通知栏都没有改变,我有同样的情况,解决方案和我一样非常简单。看到这个解决方案可能会有所帮助吗
android:fitsSystemWindows="true"