gpt4 book ai didi

android - 将导航 View 状态传播到操作布局

转载 作者:行者123 更新时间:2023-11-30 00:43:05 24 4
gpt4 key购买 nike

我正在使用 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 无效。请帮助我解决问题。

最佳答案

  • nav_header_state_list.xml 中的 state_checked 替换为 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;
}
}

关于android - 将导航 View 状态传播到操作布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42248938/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com