简体   繁体   English

带有 actionLayout 的 NavigationView 菜单项。 如何为每个项目设置这些动作布局的属性?

[英]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 变量更改每个lockedIconImageView (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) ,或者这是一个坏主意,我应该再做一次方法?

EDIT:编辑:

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.

相关问题 如果我在回收器视图中有数千个项目,那么如何在每个项目上设置项目点击侦听器 - If I Have thousands of items inside the recycler view then How can I set the item click listener on each item 如何在每个 NavigationView 菜单组设置自己的样式 - How to set their own style at each NavigationView menu group 如何设置一个微调框的选定项目控制其他微调框的项目? - How can i set one spinner's selected item control other spinner's items? 如何将自定义布局设置为菜单项? - How to set custom layout to item of menu? 如何在NavigationView的xml文件中更改菜单包含项的标题颜色 - How to change title color of menu's enclosing item in NavigationView's xml files 如何为表格中的每个项目添加上下文菜单? - How can I add a contextual menu for each item on a table? 如何在 NavigationView 中以编程方式更改 MenuItem 的标题? - How can I change MenuItem's title programatically inside NavigationView? DrawerLayout 和 NavigationView - 找不到属性“菜单” - DrawerLayout and NavigationView - attribute 'menu' not found 当我单击弹出菜单中的项目时,如何为 AlertDialog 扩大布局? - How can i inflat layout for AlertDialog when i click item in popup menu? 如何改进隐藏/显示NavigationView菜单项的方法? - How to Improve the way to Hide/Show NavigationView menu Items?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM