如何将 RelativeLayout 添加为菜单项 - Android
How to add a RelativeLayout as menu item - Android
我有一个 XML 菜单,其中有一个 item
和一个 group
。我打算在 XML 中添加一个 RelativeLayout
作为菜单项,这样它看起来像:
<menu>
<item1/>
<item2
<RelativeLayout>
<TextView1/>
<TextView2/>
<RelativeLayout>
/>
</menu>
这可以在布局 XML 中或以编程方式完成吗?如果没有,解决方法会有所帮助。谢谢。
创建一个包含您想要的视图的布局文件,然后像这样使用它 -
<item
android:id="@+id/menu_refresh"
android:title="@string/refresh"
yourapp:showAsAction="never"
android:actionLayout="@layout/my_custom_layout"/>
编辑文本视图 -
@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;
}
不能直接放在menu.xml中,必须使用android:actionLayout属性。
它是这样工作的:
menu.xml
<menu>
<item ... android:actionLayout="@layout/custom_layout"/>
</menu>
custom_layout.xml
<RelativeLayout ...>
<TextView .../>
<TextView .../>
<RelativeLayout>
要点:
要使用 Relativelayout
或任何其他布局,您必须使用 actionLayout
作为@Jack 的弱智代码答案。但请记住这一点。
android:actionLayout="@layout/my_custom_layout"
不行你必须使用 app:actionLayout="@layout/my_custom_layout"
因为如果您使用 ActionbarSherlock
或 AppCompat
,android:
命名空间将不适用于 MenuItems
。这是因为这些库使用模仿 Android API 的自定义属性,因为它们在框架的早期版本中不存在。
我有一个 XML 菜单,其中有一个 item
和一个 group
。我打算在 XML 中添加一个 RelativeLayout
作为菜单项,这样它看起来像:
<menu>
<item1/>
<item2
<RelativeLayout>
<TextView1/>
<TextView2/>
<RelativeLayout>
/>
</menu>
这可以在布局 XML 中或以编程方式完成吗?如果没有,解决方法会有所帮助。谢谢。
创建一个包含您想要的视图的布局文件,然后像这样使用它 -
<item
android:id="@+id/menu_refresh"
android:title="@string/refresh"
yourapp:showAsAction="never"
android:actionLayout="@layout/my_custom_layout"/>
编辑文本视图 -
@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;
}
不能直接放在menu.xml中,必须使用android:actionLayout属性。
它是这样工作的:
menu.xml
<menu>
<item ... android:actionLayout="@layout/custom_layout"/>
</menu>
custom_layout.xml
<RelativeLayout ...>
<TextView .../>
<TextView .../>
<RelativeLayout>
要点:
要使用 Relativelayout
或任何其他布局,您必须使用 actionLayout
作为@Jack 的弱智代码答案。但请记住这一点。
android:actionLayout="@layout/my_custom_layout"
不行你必须使用 app:actionLayout="@layout/my_custom_layout"
因为如果您使用 ActionbarSherlock
或 AppCompat
,android:
命名空间将不适用于 MenuItems
。这是因为这些库使用模仿 Android API 的自定义属性,因为它们在框架的早期版本中不存在。