Toolbar Spinner不显示文本

时间:2016-06-17 13:38:33

标签: android android-spinner android-toolbar

已更新
我需要在Spinner中设置一个Toolbar。低于AppBarLayout xml:

<android.support.design.widget.AppBarLayout
            android:id="@+id/appBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="pin"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

                <Spinner
                    android:id="@+id/toolbarSpinner"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:popupTheme="@style/AppTheme.PopupOverlay"/>

            </android.support.v7.widget.Toolbar>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/path"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_gravity="bottom"
                android:clipToPadding="false"
                app:layout_scrollFlags="snap"/>

        </android.support.design.widget.AppBarLayout>

应用程序样式:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/primaryDark</item>
        <item name="colorAccent">@color/accent</item>
    </style>

    <style name="AppTheme.SolidStatusBar" parent="AppTheme" />

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
        <item name="colorControlHighlight">@color/black_30</item>
    </style>

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

下拉标题xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@android:id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawablePadding="8dp"
        android:drawableRight="@drawable/ic_spinner_triangle"
        android:drawableEnd="@drawable/ic_spinner_triangle"
        android:fontFamily="sans-serif"
        android:paddingLeft="16dp"
        android:paddingRight="4dp"
        android:paddingStart="16dp"
        android:paddingEnd="4dp"
        android:textColor="@color/white"
        android:textSize="18sp"
        android:textStyle="bold"/>

</LinearLayout>

下拉项目:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:drawablePadding="8dp"
        android:gravity="center_vertical|start"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:textColor="#ff333333"
        android:textSize="16sp"/>

</LinearLayout>

此处当前结果(Spinner未显示文字)。我做错了什么? Non-dropdown Dropdown


适配器

public class ToolbarSpinnerAdapter extends BaseAdapter {

    private static final String TAG = ToolbarSpinnerAdapter.class.getSimpleName();

    private List<Item> mItems = new ArrayList<>();
    private LayoutInflater mInflater;

    public ToolbarSpinnerAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
    }

    public void clear() {
        mItems.clear();
    }

    public void add(final Item item) {
        mItems.add(item);
    }

    public void addAll(List<Item> items) {
        mItems.addAll(items);
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public Object getItem(final int position) {
        return mItems.get(position);
    }

    @Override
    public long getItemId(final int position) {
        return position;
    }

    @Override
    public View getDropDownView(final int position, View view, ViewGroup parent) {

        if (view == null || !view.getTag().toString().equals("DROPDOWN")) {
            view = mInflater.inflate(R.layout.toolbar_si_dropdown, parent, false);
            view.setTag("DROPDOWN");
        }

        TextView textView = (TextView) view.findViewById(android.R.id.text1);
        textView.setText(getTitle(position));

        return view;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {

        if (view == null || !view.getTag().toString().equals("NON_DROPDOWN")) {
            view = mInflater.inflate(R.layout.toolbar_si, parent, false);
            view.setTag("NON_DROPDOWN");
        }

        TextView textView = (TextView) view.findViewById(android.R.id.text1);
        textView.setText("Test");
        return view;
    }

    private String getTitle(final int position) {
        return position >= 0 && position < mItems.size() ? mItems.get(position).mTitle : "";
    }

    public static final class Item {
        private String mTitle;
        private Object mTag;

        public Item(final String title) {
            this.mTitle = title;
        }

        public String getTitle() {
            return mTitle;
        }

        public Item setTitle(final String title) {
            mTitle = title;
            return this;
        }

        @Nullable
        public Object getTag() {
            return mTag;
        }

        public Item setTag(@Nullable Object tag) {
            mTag = tag;
            return this;
        }
    }
}

ADDED
我理解我的错误。在适配器中不调用方法public View getView(final int position, View view, ViewGroup parent)(仅适用于下拉视图)。魔术背后可能是什么? (我在LogCat中查看过。)

2 个答案:

答案 0 :(得分:1)

我认为你不想为你Spinner设置主题,因为它应该从你Toolbar中指定的主题叠加层继承其属性。但是,我不认为它继承了您指定的弹出主题,因此您也应该在Spinner中设置它。此外,您无需额外LinearLayout来包装Spinner。您可以使用Spinner作为xml中的根对象:

<Spinner xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/spinner"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    app:popupTheme="@style/AppTheme.PopupOverlay"/>

或直接将其放在Toolbar

<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
    android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary" android:fitsSystemWindows="true"
    app:layout_scrollFlags="scroll|exitUntilCollapsed" app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:layout_collapseMode="pin" app:popupTheme="@style/AppTheme.PopupOverlay">

    <Spinner android:id="@+id/spinner"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.v7.widget.Toolbar>

最后,请确保您的Toolbar

中没有显示标题
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);

答案 1 :(得分:0)

我发现了这个错误。问题是Spinner视图如果在创建时它的适配器为空,将无法正常工作。要在我们将在适配器中添加新项目时修复此问题,我们将再次重新设置适配器,并调用spinner.setAdapter(adapter);