安卓(Android)中的 Spinner 工具栏样式

29

如何在工具箱中实现Spinner的Material Design?我尝试了以下方法,但我的Spinner看起来像这样

输入图片说明

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:background="@color/main_color"
    app:contentInsetLeft="14dp"
    app:contentInsetRight="14dp"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/ThemeOverlay.AppCompat.ActionBar">

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

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

我的最低SDK版本是15。

在我的onCreate方法中

ButterKnife.inject(this);

    mToolbar.inflateMenu(R.menu.menu_login);
    mToolbar.setTitle(R.string.sign_in);
    mToolbar.setOnMenuItemClickListener(this);

    SpinnerAdapter spinnerAdapter = ArrayAdapter.createFromResource(this,
            R.array.options, android.R.layout.simple_spinner_dropdown_item);

    mSpinner.setAdapter(spinnerAdapter);
    mSpinner.setOnItemSelectedListener(this);

菜单是一张临时纸,始终与应用栏重叠,而不是作为应用栏的延伸。

任何链接都将是很大的帮助。

更新

我已经成功实现了我的目标

输入图像描述

我使用了这个

 ButterKnife.inject(this);

        mToolbar.setTitle(R.string.sign_in);
        setSupportActionBar(mToolbar);

        SpinnerAdapter spinnerAdapter = ArrayAdapter.createFromResource(getSupportActionBar().getThemedContext(),
                R.array.options, android.R.layout.simple_spinner_dropdown_item);

        getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        getSupportActionBar().setListNavigationCallbacks(spinnerAdapter, this);

和我的 XML

  <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="@color/app_secondary_color"
        app:contentInsetLeft="14dp"
        app:contentInsetRight="14dp"
        app:popupTheme="@style/ThemeOverlay.AppCompat"
        app:theme="@style/ThemeOverlay.AppCompat.ActionBar">

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

那么,如何使它变为白色,菜单文字的颜色为蓝色呢?另外,我还有一个问题。它似乎太远了?有没有办法修复下拉箭头?

enter image description here

更新2

尝试了很长时间后,我通过创建自定义下拉样式来实现了将其变为白色且文本颜色为黑色。

<?xml version="1.0" encoding="utf-8"?>

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:background="@color/background_material_light"
    android:textColor="@color/abc_primary_text_material_light"
    android:layout_height="48dp"
    android:ellipsize="marquee"/>

然后我在我的oncreate中添加了这段代码

 spinnerAdapter.setDropDownViewResource(R.layout.custom_simple_spinner_dropdown_item);
现在我遇到的唯一问题是按下状态不起作用,只是一个空白。如果您在另一个弹出菜单中看到,当您单击某个项目时,背景颜色会变得稍微深一些。此外,这个烦人的图标仍然离文本太远。

你想要实现哪一个,左边还是右边? - tyczj
你尝试将基础主题更改为Light了吗? - waqaslam
是的,但是旋转菜单仍然没有实现材料化。 - Milk
7
请勿编辑帖子并附上解决方案。如果您已经找到了解决方案本身,请发布为回答。 - Machado
你应该在这里查看我的答案:https://dev59.com/BIfca4cB1Zd3GeqPhlcr#30023348 - user3426273
显示剩余2条评论
4个回答

1

如果您想让按下状态起作用,您应该添加一个Drawable选择器而不是颜色作为背景。选择器示例:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/numpad_button_bg_selected" android:state_selected="true"></item>
    <item android:drawable="@drawable/numpad_button_bg_pressed" android:state_pressed="true"></item>
    <item android:drawable="@drawable/numpad_button_bg_normal"></item>

</selector>

0

尝试使用以下代码更改Spinner的第一个项目字体颜色或背景颜色

Spinner spinner = findViewById(R.id.spn);
        ArrayList<String> lst = new ArrayList<>();
        lst.add("Android");
        lst.add("Iphone");
        lst.add("Windows");

        ArrayAdapter<String> adapter = new ArrayAdapter<String >(getApplicationContext(),R.layout.spin_item,lst)
        {
            public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
            {
                View v = null;

                if (position == 0) {
                    TextView tv = new TextView(getContext());
                    tv.setTextColor(Color.parseColor("#007f00"));
                    v = tv;
                }
                else {

                    v = super.getDropDownView(position, null, parent);
                }
                return v;
            }
        };
        spinner.setAdapter(adapter);

0
你应该创建一个颜色状态列表资源并将其应用为CheckedTextView的背景。至于箭头图标太远的问题,据我所知,这是由于子项的长度决定的,它会延伸到最长的子项。因此,无法自定义它。

0
我们可以使用PopupWindowshowAsDropDown()方法在给定视图上方显示视图/下拉列表。
 LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
                @SuppressLint("InflateParams")
                View popupView = layoutInflater.inflate(R.layout.menu_logout, null);
                PopupWindow popupWindow = new PopupWindow(popupView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                popupWindow.setOutsideTouchable(true);
                popupWindow.setFocusable(true);
                popupWindow.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
                popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                popupWindow.showAsDropDown(logoutParent);

                TextView textView = popupView.findViewById(R.id.menu);
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        doLogoutWork();

                    }
                });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接