[英]NavigationView menu items with actionLayout. How can I set those action layout's attribute for each item?
Following this answer , I learned how to add extra views to the NavigationView
's menu items, by adding an action layout
.按照这个答案,我学习了如何通过添加
action layout
来向NavigationView
的菜单项添加额外的视图。 My question is, esentially, how can I tweak each of those individual actionLayout
s dinamically, through Java?我的问题是,基本上,我如何通过 Java 动态地调整每个
actionLayout
?
In my case, instead of adding a "switch" to the menu items, I used actionLayout
s to add a extra icon, that will show a state for that item.就我而言,我没有向菜单项添加“开关”,而是使用
actionLayout
添加了一个额外的图标,该图标将显示该项目的 state。 It's a boolean state, so I want to show the difference either by changing the extra icon dinamically, or either toggling visibility.这是一个 boolean state,所以我想通过动态更改额外图标或切换可见性来显示差异。
So, I have a menu.xml
like this one for my NavigationView
:所以,我的
NavigationView
有一个menu.xml
像这个:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:showIn="navigation_view">
<group android:id="@+id/menu_group"
android:checkableBehavior="single" >
<item
android:id="@+id/menu_item1"
android:icon="@drawable/some_main_icon"
app:actionLayout="@layout/myflag_actionlayout"
android:title="OPTION 1" />
<item
android:id="@+id/menu_item2"
android:icon="@drawable/some_main_icon"
app:actionLayout="@layout/myflag_actionlayout"
android:title="OPTION 2" />
<!-- etc. -->
</group>
</menu>
...and my myflag_actionlayout.xml
file: ...和我的
myflag_actionlayout.xml
文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#444444"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:id="@+id/lockedIcon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:srcCompat="@android:drawable/ic_lock_idle_lock" />
</LinearLayout>
So, it looks like this more or less:所以,它或多或少看起来像这样:
Now I wonder: how can I get each of those menu item's instances, access to their <ImageView>
instance from Java and change each lockedIcon
's ImageView
depending on a boolean variable?现在我想知道:如何获取每个菜单项的实例,从 Java 访问它们的
<ImageView>
实例并根据 Z84E2C64F38F78BA3EA5C905ABZA 变量更改每个lockedIcon
的ImageView
? (For instance, toggling the ImageView
's visible attribute, or changing the image for app:srcCompat
attribute) Should I use some findViewById(R.id.lockedIcon)
for this task, or is it a bad idea and I should do it another way? (例如,切换
ImageView
的可见属性,或更改app:srcCompat
属性的图像)我是否应该为此任务使用一些findViewById(R.id.lockedIcon)
,或者这是一个坏主意,我应该再做一次方法?
Just now I recall, I already did something similar to access those " NavigationView
items" by Java, and add a string to the counter editing the title:刚才我记得,我已经做了类似的事情来访问 Java 的那些“
NavigationView
项目”,并在编辑标题的计数器中添加一个字符串:
NavigationView nav_v = findViewById(R.id.navigation_view);
MenuItem nav_option1 = nav_v.getMenu().findItem(R.id.menu_item1);
nav_option1.setTitle(getString(R.string.nav_option1_title) + " -> " + some_counter);
Maybe i can from nav_option1
invoke some method to access it's action layout?也许我可以从
nav_option1
调用一些方法来访问它的动作布局?
Good.好的。 @ianhanniballake 's suggested method worked like a charm: If I do this:
@ianhanniballake 的建议方法就像一个魅力:如果我这样做:
NavigationView nav_v = findViewById(R.id.navigation_view);
MenuItem nav_option1 = nav_v.getMenu().findItem(R.id.menu_item1);
ImageView option1_lock_icon = nav_option1.getActionView().findViewById(R.id.lockedIcon);
And from there, I could do whatever I want with this ImageView
:从那里,我可以用这个
ImageView
做任何我想做的事情:
// Toggle visibility approach
if (item_locked) {
option1_lock_icon.setVisibility(View.VISIBLE);
} else {
option1_lock_icon.setVisibility(View.INVISIBLE);
}
// Change icon approach
string uri = "";
if (item_locked) {
uri = "@android:drawable/ic_lock_idle_lock";
} else {
uri = "@android:drawable/ic_menu_view";
}
int imageResource = getResources()
.getIdentifier(uri,
null,
getPackageName()
);
option1_lock_icon.setImageDrawable(
getResources().getDrawable(imageResource)
);
Also, it looks like I can do more or less the same with any object I add to those "action layouts".此外,看起来我可以或多或少地对我添加到那些“动作布局”的任何 object 做同样的事情。 :-)
:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.