如何访问视图,包括菜单中的actionLayout [英] How to access views insde the actionLayout in menu

查看:43
本文介绍了如何访问视图,包括菜单中的actionLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个包含同一个app:actionLayout的菜单项,它具有一个textview,如何通过代码分别访问textview并为所有三个textview设置不同的文本.

I have three menu items which includes same app:actionLayout it has a textview, how can i access the textviews individually through code and set different text for all three textviews.

activity_main_drawer.xml

activity_main_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/item_tracks"
            app:actionLayout="@layout/menu_text_layout"
            android:icon="@drawable/ic_play_arrow"
            android:title="Tracks"/>
        <item
            android:id="@+id/item_repeat"
            app:actionLayout="@layout/menu_text_layout"
            android:icon="@drawable/ic_play_arrow"
            android:title="Repeat"/>
        <item
            android:id="@+id/item_timer"
            app:actionLayout="@layout/menu_text_layout"
            android:icon="@drawable/ic_play_arrow"
            android:title="Timer"/>
    </group>

    <item android:title="More">
        <menu>
            <item
                android:id="@+id/item_animation"
                app:showAsAction="always"
                android:icon="@drawable/ic_play_arrow"
                app:actionLayout="@layout/switch_layout"
                android:title="Animation"/>
            <item
                android:id="@+id/item_background"
                android:icon="@drawable/ic_play_arrow"
                android:title="Background Music"/>
        </menu>
    </item>

</menu>

menu_text_layout.xml

menu_text_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tool="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tool:background="@android:color/white"
    android:gravity="center_vertical">

    <TextView
        android:id="@+id/switchForActionBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:maxLength="10"
        android:ellipsize="marquee"
        android:fontFamily="sans-serif"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:background="@color/Color_600"
        android:textColor="@android:color/white"
        android:padding="2.5dp"
        android:text="Change Text"/>

</LinearLayout>

推荐答案

OnCreateOptionsMenu中看起来像这样:

getMenuInflater().inflate(R.menu.activity_main_drawer, menu);

LinearLayout tracks = (LinearLayout) menu.findItem(R.id.item_tracks).getActionView();
LinearLayout repeat = (LinearLayout) menu.findItem(R.id.item_repeat).getActionView();
LinearLayout timer = (LinearLayout) menu.findItem(R.id.item_timer).getActionView();

TextView tvTracks = (TextView) tracks.findViewById(R.id.switchForActionBar);
TextView tvRepeat = (TextView) repeat.findViewById(R.id.switchForActionBar);
TextView tvTimer = (TextView) timer.findViewById(R.id.switchForActionBar);

tvTracks.setText("foo");
tvRepeat.setText("bar");
tvTimer.setText("baz");
return true;

我正在使用LinearLayout,因为它是menu_text_layout.xml中的第一个元素

I'm using LinearLayout as it's the first element in menu_text_layout.xml

这篇关于如何访问视图,包括菜单中的actionLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆