自定义微调器背景在lolipop设备上不起作用

时间:2018-11-16 07:53:38

标签: android android-spinner

我创建了一个自定义spinner,它可以与除Android 5.0以外的所有设备完美配合

这是我的微调背景代码

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item  >

        <layer-list>
            <item>
                <shape>
                    <gradient
                        android:startColor="#fff"
                        android:endColor="#fff"
                        android:angle="270" />
                    <stroke
                        android:width="0.7dp"
                        android:color="#000" />
                    <corners
                        android:radius="40dp" />
                    <padding
                        android:left="3dp"
                        android:top="3dp"
                        android:right="3dp"
                        android:bottom="3dp" />
                </shape>
            </item>

            <item
                android:gravity="end"
                android:right="20dp"
                android:top="@dimen/height_10"
                android:width="@dimen/height_20"
                android:height="@dimen/height_20"
                android:drawable="@drawable/ic_drop" >

            </item>
        </layer-list>
    </item>
</selector>

这是lolipop设备以外的外观

除了lolipop

other than lolipop

在lolipop设备上

on lolipop

layout.xml

   <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="@drawable/loginbg"
    android:layout_height="match_parent"
    >


            <Spinner
                style="@style/SpinnerStyle"
                android:id="@+id/spin_seat_count"
                android:layout_width="match_parent"
                android:layout_height="@dimen/height_50">
            </Spinner>


    </LinearLayout>

</RelativeLayout>

我已更新查询..请检查布局xml

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码创建视图,该视图适用于所有设备。

首先在drawable中创建round.xml

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <!-- view background color -->
    <!--<solid
        android:color="@color/color_gray" >
    </solid>-->

    <stroke
        android:width="@dimen/_1sdp"
        android:color="@color/light_gray"
        />

    <padding
    android:left="0dp"
    android:top="0dp"
    android:right="0dp"
    android:bottom="0dp" />


    <!-- Here is the corner radius -->
    <corners
        android:bottomRightRadius="5dp"
        android:bottomLeftRadius="5dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp"/>

</shape>

现在activity.xml中的该可绘制对象称为:

<RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/_35sdp"
                    android:layout_below="@+id/hint_categorySelection"
                    android:layout_marginTop="@dimen/_5sdp"
                    android:background="@drawable/round_edit">

                    <Spinner
                        android:id="@+id/spinnerCategory"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:layout_marginStart="@dimen/_5sdp"
                        android:layout_marginTop="@dimen/_5sdp"
                        android:layout_toStartOf="@+id/imgDrpCategory"
                        android:background="@null"></Spinner>

                    <ImageView
                        android:id="@+id/imgDrpCategory"
                        android:layout_width="@dimen/_20sdp"
                        android:layout_height="@dimen/_20sdp"
                        android:layout_alignParentEnd="true"
                        android:layout_centerInParent="true"
                        android:layout_marginEnd="@dimen/_3sdp"
                        android:layout_marginStart="@dimen/_2sdp"
                        android:paddingEnd="@dimen/_5sdp"
                        android:src="@drawable/ic_drop_down" />
                </RelativeLayout>

对于Open Spinner,请使用以下代码:

imgDrpCategory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 spinnerCategory.performClick();
            }
        });

输出:

enter image description here