How to access views insde the actionLayout in menu

前端 未结 1 685
攒了一身酷
攒了一身酷 2021-02-03 12:35

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

相关标签:
1条回答
  • 2021-02-03 12:50

    It would look something like this 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;
    

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

    0 讨论(0)
提交回复
热议问题