【问题标题】:Spinner in ActionBar doesn't appearActionBar 中的微调器不出现
【发布时间】:2013-03-21 07:52:58
【问题描述】:

我已经实现了像 here 这样的微调器。现在我想将微调器放在 ActionBar 中(将微调器设置为 CustomView 到 actionBar),但它没有出现在那里:(

Spinner spinner = (Spinner) findViewById(R.id.planets_spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.planets_array, android.R.layout.simple_spinner_item);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

        spinner.setAdapter(adapter);

        actionBar.setCustomView(spinner);

这里是 .XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/parkfragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <Spinner
        android:id="@+id/planets_spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

请不要建议使用 ActionBar DropDown List,我不能使用它,因为我已经在使用 ActionBar Tabs。 android不允许同时使用它们。 任何帮助将不胜感激

【问题讨论】:

    标签: android android-layout android-actionbar android-spinner


    【解决方案1】:

    您的代码缺少这一行:

    actionBar.setDisplayShowCustomEnabled(true); 
    

    参考ActionBar#setDisplayShowCustomEnabled(boolean showCustom)。


    更新:

    为什么你的布局中有一个空的FrameLayout 占据了所有的宽度和高度?去掉它。并删除LinearLayout。只需将其用作布局:

    <?xml version="1.0" encoding="utf-8"?>
    <Spinner xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/planets_spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" /> 
    

    然后写这样的东西,一切都很好:

    actionBar.setCustomView(R.layout.your_layout_with_the_spinner);
    actionBar.setDisplayShowCustomEnabled(true);
    
    Spinner spinner = (Spinner) findViewById(R.id.planets_spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.planets_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    

    【讨论】:

    • 添加这行代码后,应用程序崩溃并出现此错误You must call removeView() on the child's parent first.
    • 非常感谢! :) 它帮助了我 :) 我还有一个问题,如何将微调器粘贴到 ActionBar 的右侧?我尝试使用android:layout_alignParentRight="true",但没有成功。
    • 只有使用自定义 action 视图才有可能。但这是一种不同的方法。阅读更多here。
    猜你喜欢
    • 2012-02-15
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 2012-03-12
    • 1970-01-01
    相关资源
    最近更新 更多