我有一个带有 item
和 group
的菜单 XML。我打算在 XML 中添加一个 RelativeLayout
作为菜单项,这样它看起来像:
<menu>
<item1/>
<item2
<RelativeLayout>
<TextView1/>
<TextView2/>
<RelativeLayout>
/>
</menu>
这可以在布局 XML 中完成,还是以编程方式完成?如果没有,解决方法会有所帮助。谢谢。
用你想要的 View 创建一个布局文件,然后像这样使用它 -
<item
android:id="@+id/menu_refresh"
android:title="@string/refresh"
yourapp:showAsAction="never"
android:actionLayout="@layout/my_custom_layout"/>
编辑 TextView -
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
//Get a reference to your item by id
MenuItem item = menu.findItem(R.id.menu_refresh);
//Here, you get access to the view of your item, in this case, the layout of the item has a RelativeLayout as root view but you can change it to whatever you use
RelativeLayout rootView = (RelativeLayout)item.getActionView();
//Then you access to your control by finding it in the rootView
TextView textview1 = (TextView) rootView.findViewById(R.id.text1);
//And from here you can do whatever you want with your text view
return true;
}
我是一名优秀的程序员,十分优秀!