2015-12-20 32 views
6

Ho tre voci di menu che includono lo stesso app:actionLayout ha una vista testuale, come posso accedere alle visualizzazioni di testo singolarmente tramite codice e impostare un testo diverso per tutte e tre le visualizzazioni di testo.Come accedere alle viste nell'actionLayout nel menu

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

<?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> 

risposta

6

Sarebbe simile a questa in 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; 

sto usando LinearLayout in quanto è il primo elemento in menu_text_layout.xml

Problemi correlati