由网友(Luminary@(发光体))分享简介:findViewById()当你需要找到的东西,是在布局的UI元素工作正常。但是,使用这个答案如何加入在操作栏一个下拉的项目,我添加了一个微调我的动作条。 现在我面临的问题是如何检索到这个微调的参考,这样我可以更改字体,它的用途。 如何做呢? 我的菜单/ sort_spinner 是这样的: <菜单的xmln...![NativeScript 之 ActionBar](https://p.xsw88.cn/allimgs/daicuo/20230906/1717.png)
findViewById()
当你需要找到的东西,是在布局的UI元素工作正常。但是,使用这个答案如何加入在操作栏一个下拉的项目,我添加了一个微调
我的动作条
。
现在我面临的问题是如何检索到这个微调
的参考,这样我可以更改字体,它的用途。 如何做呢?
我的菜单/ sort_spinner 是这样的:
<菜单的xmlns:机器人=http://schemas.android.com/apk/res/android> <项目机器人:ID =@ + ID /排序 机器人:showAsAction =总是 机器人:标题=@字符串/排序 机器人:actionLayout =@布局/ actionbar_spinner/>&所述; /菜单>
和布局/ actionbar_spinner 是这样的:
<微调的xmlns:机器人=http://schemas.android.com/apk/res/android 机器人:layout_width =match_parent 机器人:layout_height =match_parent 机器人:重力=CENTER_HORIZONTAL 机器人:spinnerMode =下拉菜单 机器人:ID =@ + ID /飞旋 机器人:项=@阵列/输入/>
![NativeScript 之 ActionBar](https://p.xsw88.cn/allimgs/daicuo/20230906/1717.png)
解决方案
您必须这样做,在公共布尔onCreateOptionsMenu(菜单菜单)
。
@覆盖公共布尔onCreateOptionsMenu(菜单菜单){ 。getMenuInflater()膨胀(R.menu.sort_spinner,菜单); 菜单项spinnerItem = menu.findItem(R.id.sort); 微调微调=(微调)spinnerItem.getActionView()findViewById(R.id.spinner)。 //你的code休息... 返回super.onCreateOptionsMenu(菜单);}
findViewById()
works fine when you need to find something any UI element that is in your layout. However, using the second option of this answer How to add a Dropdown item on the action bar, I added a Spinner
to my ActionBar
.
Now the problem I am facing is how to retrieve a reference to this Spinner
so that I can change the font that it uses. How do I do that?
My menu/sort_spinner looks like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/sort"
android:showAsAction="always"
android:title="@string/sort"
android:actionLayout="@layout/actionbar_spinner"/>
</menu>
and layout/actionbar_spinner like this:
<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:spinnerMode="dropdown"
android:id="@+id/spinner"
android:entries="@array/type" />
解决方案
You have to do it in the public boolean onCreateOptionsMenu(Menu menu)
.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.sort_spinner, menu);
MenuItem spinnerItem = menu.findItem(R.id.sort);
Spinner spinner = (Spinner)spinnerItem.getActionView().findViewById(R.id.spinner);
//Your rest of code...
return super.onCreateOptionsMenu(menu);
}
相关推荐
最新文章