我需要在运行时设置DrawerLayout的背景色。这是我在做什么:
DrawerLayout drawerLayout = findViewById(R.id.drawer_layout);
drawerLayout.setBackgroundColor(Color.parseColor("#ffffff"));
我不会真正将其设置为硬编码值,仅用于说明。这似乎没有作用-背景保持深灰色,我认为是从某处的样式中拾取的。我可以在DrawerLayout中设置ExpandableListView的背景颜色,但无法填充垂直空间,因此某些背景是正确的颜色,空白区域是错误的颜色。
在XML中设置ExpandableListView的背景色是可行的,但是我需要能够在运行时进行更改。如果未在XML中设置ListView颜色,则ListView将不会像我想要的那样填充垂直空间。显示所有项目后,它会自动关闭。我试过在Java中同时设置抽屉布局和列表背景,而最能产生效果的是列表项下方的空白会更改颜色。我无法更改实际列表的背景。有什么建议吗?
布局XML:
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activities.MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/primary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ToolBarStyle" />
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</FrameLayout>
<!-- The navigation drawer -->
<ExpandableListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/drawer_background"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:groupIndicator="@null" />
</android.support.v4.widget.DrawerLayout>
屏幕截图:
答案 0 :(得分:1)
我认为您需要为NavigationView
而不是DrawerLayout
设置背景。
<android.support.design.widget.NavigationView
android:id="@+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white" //set background here
app:itemBackground="@android:color/transparent"
app:menu="@menu/menu_navigation"/>
如果您在ExpandableListView
中使用DrawerLayout
。我认为您应该将ExpandableListView
放在RelativeLayout
中。像这样:
<android.support.v4.widget.DrawerLayout
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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activities.MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/primary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ToolBarStyle" />
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</FrameLayout>
<!-- The navigation drawer -->
<RelativeLayout
android:id="@+id/drawer_container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@drawable/background"> //set background here
<ExpandableListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/drawer_background"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:groupIndicator="@null" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>