将导航视图状态传播到操作布局

时间:2017-02-15 12:06:44

标签: android navigation-drawer android-design-library navigationview android-navigationview

我正在使用android支持库来创建导航抽屉。

<android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        android:background="@color/white"
        app:headerLayout="@layout/nav_header_landing"
        app:menu="@menu/activity_dashboard_drawer"
        app:theme="@style/MyActionButtonStyle"/>

我正在使用actionLayout

创建自定义菜单项
<group android:checkableBehavior="single"
        android:id="@+id/gp_dashboard">
        <item
            android:id="@+id/nav_dashboard"
            app:actionLayout="@layout/layout_dashboard"
            android:icon="@mipmap/ic_dashboard"
            android:title=""/>
    </group>

这是布局"layout_dashboard"

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:duplicateParentState="true">

    <helpers.customViews.RobotoTextView
        android:id="@+id/dash_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="DASHBOARD"
        app:typeface="robotoMedium"
        android:textColor="@color/nav_header_state_list"
        android:textSize="14sp"/>

    <helpers.customViews.RobotoTextView
        android:id="@+id/dash_subtitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/dash_title"
        android:text="@string/dashboard_txt"
        app:typeface="robotoRegular"
        android:textColor="@color/nav_sub_header_state_list"
        android:textSize="12sp"/>

</RelativeLayout>

现在,我想在选择菜单项时更改此布局的文本颜色。为此,我使用@color/nav_header_state_list

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/greeny_blue" android:state_checked="true"/>
    <item android:color="@color/greeny_blue" android:state_pressed="true" />
    <item android:color="@color/black"/>
</selector>

但即使选择了菜单项,颜色也保持不变。 pressed state有效但checked state没有。请帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

  • state_checked替换为 nav_header_state_list.xml
  • 中的state_selected

您的 nav_header_state_list.xml 应如下所示:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/greeny_blue" android:state_pressed="true" />
    <item android:color="@color/greeny_blue" android:state_selected="true" />
    <item android:color="@color/black" />
</selector>
  • MainActivity.java
  • 中声明MenuItem个对象
  • 将以下逻辑添加到onNavigationItemSelected

您的 MainActivity.java 应如下所示:

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    MenuItem previousItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // ... some code
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        // ... some code

        if (previousItem != null) {
            View previousActionView = previousItem.getActionView();
            previousActionView.findViewById(R.id.dash_title).setSelected(false);
            previousActionView.findViewById(R.id.dash_subtitle).setSelected(false);
        }
        View actionView = item.getActionView();
        actionView.findViewById(R.id.dash_title).setSelected(true);
        actionView.findViewById(R.id.dash_subtitle).setSelected(true);
        previousItem = item;

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}