gpt4 book ai didi

android - 如何将 RelativeLayout 添加为菜单项 - Android

转载 作者:太空宇宙 更新时间:2023-11-03 12:15:27 24 4
gpt4 key购买 nike

我有一个带有 itemgroup 的菜单 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;
}

关于android - 如何将 RelativeLayout 添加为菜单项 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38495851/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com