Android - 更改自定义 Spinner 的 DropDownItem 样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24878267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:53:07  来源:igfitidea点击:

Android - Change Custom Spinner's DropDownItem style

androidandroid-layoutandroid-spinnerandroid-styles

提问by ThomQ

I got a custom spinner, and I'm trying to replace the 9-patch background / the triangle in the DropDownSelector.

我有一个自定义微调器,我正在尝试替换 DropDownSelector 中的 9-patch 背景/三角形。

I just can't get it to work right though. I end up with (the white box is a test asset):

我只是无法让它正常工作。我最终得到(白框是测试资产):

enter image description here

在此处输入图片说明

the new 9 patch gets shown, but it messes up the padding, and it looks like a Spinner inside a Spinner.

显示了新的 9 补丁,但它弄乱了填充,看起来像 Spinner 里面的 Spinner。

Here's what it looks like without the 9 patch added:

这是没有添加 9 补丁的情况:

enter image description here

在此处输入图片说明

This is what I want it to look like, but then with the new 9patch instead of the old one, not the Spinner in Spinner effect.

这就是我想要的样子,但是使用新的 9patch 而不是旧的,而不是 Spinner 效果中的 Spinner。

Here's my code:

这是我的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

   <Spinner xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/spinner2"
             android:layout_width="wrap_content"
             android:layout_height="fill_parent"
             android:layout_alignParentRight="true"
             android:gravity="center_horizontal"/>

</RelativeLayout>

I add this RelativeLayout to the Actionbar, and set a custom spinner adapter:

我将此 RelativeLayout 添加到操作栏,并设置自定义微调适配器:

    SpinnerAdapter mSpinnerAdapter = (new SpinnerCustomAdapterDark(this, R.layout.customSpinnerTitleLayout, categoryNames ));

    spinner = findViewById(R.id.spinner2);

    categorySpinnerMenuitem = (Spinner) spinner;

    categorySpinnerMenuitem.setAdapter(mSpinnerAdapter);

This is the CustomSpinnerTitleLayout set to the adapter:

这是设置为适配器的 CustomSpinnerTitleLayout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                style="?android:attr/spinnerDropDownItemStyle"
                android:paddingRight="0dp"   >
    <ImageView

            android:id="@+id/spinner_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/icon"
            android:layout_gravity="center"
            android:paddingRight="0dp"
            />

</LinearLayout>

This is the Theme where I add the 9 patch

这是我添加 9 补丁的主题

<resources>
    <style name="CustomTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="android:spinnerDropDownItemStyle">@style/customActionBarDropDownStyle</item>
    </style>


    <style name="customActionBarDropDownStyle" parent="@android:style/Widget.Holo.Light.ListView" >
           <item name="android:background">@drawable/spinner9patch</item>
    </style>
</resources>

I'm obviously doing something wrong, but what? I've tried to set the spinnerDropDownItemStyle and the spinnerStyle at the Spinner in the first layout file, what didn't do anything. What am I doing wrong here?

我显然做错了什么,但什么?我试图在第一个布局文件中的 Spinner 处设置 spinnerDropDownItemStyle 和 spinnerStyle ,但没​​有做任何事情。我在这里做错了什么?

Thanks in advance!!

提前致谢!!

回答by Aniruddha

Create an XML in drawable folder with any name for example spinner_bg.xmland add the following lines

例如spinner_bg.xml,在 drawable 文件夹中创建一个任何名称的 XML并添加以下几行

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

  <item><layer-list>
        <item><shape>
                <gradient android:angle="90" android:endColor="#ffffff" android:startColor="#ffffff" android:type="linear" />

                <stroke android:width="1dp" android:color="#504a4b" />

                <corners android:radius="5dp" />

                <padding android:bottom="3dp" android:left="3dp" android:right="3dp" android:top="3dp" />
            </shape></item>
        <item ><bitmap android:gravity="bottom|right" android:src="@drawable/spinner_ab_default_holo_dark_am" />   // you can use any other image here, instead of default_holo_dark_am
        </item>
      </layer-list></item>

 </selector>  

Add the following lines to your styles.xmlwhich is inside values folder

将以下行添加到您的styles.xml值文件夹中

  <style name="spinner_style" >
        <item name="android:background">@drawable/spinner_bg</item>
        <item name="android:layout_marginLeft">10dp</item>
        <item name="android:layout_marginRight">10dp</item>
        <item name="android:layout_marginBottom">10dp</item>
        <item name="android:paddingLeft">8dp</item>
        <item name="android:paddingTop">5dp</item>
        <item name="android:paddingBottom">5dp</item>

Now add this style to your spinner as

现在将此样式添加到您的微调器中

<Spinner
            android:id="@+id/spinner1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/spinner_style"
            android:popupBackground="#cccccc" />

回答by NoXSaeeD

I really recommended you check this out online Generator just make your custom spinner then download file http://jgilfelt.github.io/android-actionbarstylegenerator/

我真的建议您在线查看 Generator 只需制作您的自定义微调器然后下载文件http://jgilfelt.github.io/android-actionbarstylegenerator/