Android的 - 更改自定义微调的DropDownItem风格 [英] Android - Change Custom Spinner's DropDownItem style

查看:181
本文介绍了Android的 - 更改自定义微调的DropDownItem风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的微调,而我试图取代9补丁背景/在DropDownSelector三角形。

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

我只是无法得到它的,虽然工作的权利。我结束了(白框是一个测试资产):

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

新的9补丁得到显示,但它搅乱了填充,它看起来像一个微调内的微调。

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

下面是什么样子,而不9补丁补充说:

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

这就是我想要的样子,但随着新9patch,而不是旧的,而不是微调的微调效果。

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.

下面是我的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>

我这个RelativeLayout的添加到动作条,并设置一个自定义的微调适配器:

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

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

    spinner = findViewById(R.id.spinner2);

    categorySpinnerMenuitem = (Spinner) spinner;

    categorySpinnerMenuitem.setAdapter(mSpinnerAdapter);

这是CustomSpinnerTitleLayout设置为适配器:

This is the CustomSpinnerTitleLayout set to the adapter:

<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>

这是我加上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>

我明明做错了什么,但什么?我试着设置spinnerDropDownItemStyle,并在第一个布局文件spinnerStyle的微调,什么也没有做任何事情。我在做什么错在这里?

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?

在此先感谢!

推荐答案

在创建绘制文件夹中的XML与任何名称,例如 spinner_bg.xml 并添加以下行

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

<?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>  

添加以下行到你的 styles.xml 这是内部价值观的文件夹

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

  <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" />

这篇关于Android的 - 更改自定义微调的DropDownItem风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆