简体   繁体   English

Android - 更改Custom Spinner的DropDownItem样式

[英]Android - Change Custom Spinner's DropDownItem style

I got a custom spinner, and I'm trying to replace the 9-patch background / the triangle in the DropDownSelector. 我有一个自定义微调器,我正在尝试替换DropDownSelector中的9补丁背景/三角形。

I just can't get it to work right though. 我只是无法让它正常工作。 I end up with (the white box is a test asset): 我最终(白盒子是测试资产):

在此输入图像描述

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补丁的情况:

在此输入图像描述

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添加到Actionbar,并设置自定义微调器适配器:

    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. 我试图在第一个布局文件中的Spinner处设置spinnerDropDownItemStyle和spinnerStyle,什么都没做。 What am I doing wrong here? 我在这做错了什么?

Thanks in advance!! 提前致谢!!

Create an XML in drawable folder with any name for example spinner_bg.xml and add the following lines 在可绘制文件夹中使用任何名称创建XML,例如spinner_bg.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.xml which is inside values folder styles.xml添加到styles.xml文件夹内的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" />

我真的建议你在网上检查这个,只需制作你的自定义微调器,然后下载文件http://jgilfelt.github.io/android-actionbarstylegenerator/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM