获取参考的ActionBar一个微调 [英] Get reference to a Spinner in ActionBar

查看:140
本文介绍了获取参考的ActionBar一个微调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

findViewById()当你需要找到的东西,是在布局的UI元素工作正常。但是,使用这个答案如何加入在操作栏一个下拉的项目,我添加了一个微调我的动作条

现在我面临的问题是如何检索到这个微调的参考,这样我可以更改字体,它的用途。 如何做呢?

我的菜单/ sort_spinner 是这样的:

 <菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目机器人:ID =@ + ID /排序
        机器人:showAsAction =总是
        机器人:标题=@字符串/排序
        机器人:actionLayout =@布局/ actionbar_spinner/>
&所述; /菜单>

布局/ actionbar_spinner 是这样的:

 <微调的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:重力=CENTER_HORIZONTAL
    机器人:spinnerMode =下拉菜单
    机器人:ID =@ + ID /飞旋
    机器人:项=@阵列/输入/>


解决方案

您必须这样做,在公共布尔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);
}

这篇关于获取参考的ActionBar一个微调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆