spinner width does not matches with spinner item w

2019-08-04 13:09发布

问题:

When I use specific width like 400dp for the DropDown item's width and spinner width it matches properly.

But When I am using match_parent for the spinner and custom_spinner_item width it does not match? Image may give an idea What is wrong with my code? I want the same width using match_parent?

This is my custom custom_spinner_item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
        android:id="@+id/textView"
        android:layout_width="400dp"            // width
        android:layout_height="30dp"
        android:textSize="18dp"
        android:text="wallets"
        android:layout_marginLeft="5dp"
        android:paddingLeft="5dp"
        android:background="#FFFFFF"
        android:textColor="#282727"/>
    <View
        android:layout_width="400dp"
        android:layout_height="1dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="#8e8c8c"/>
</LinearLayout>

My Fragment

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/appbackground"
    android:paddingRight="5dp"
    android:paddingLeft="5dp"
    android:paddingTop="5dp">   

    // some code

   <LinearLayout
        android:id="@+id/dropDownLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@drawable/zxing_roundedbgwhite"
        android:orientation="vertical">
        <Spinner
            android:id="@+id/spinner1"
            android:layout_width="400dp"        // width
            android:layout_height="35dp"
            android:dropDownWidth="400dp"       // width
            android:spinnerMode="dropdown" />
    </LinearLayout>

    // some code

</LinearLayout>

回答1:

The best way will be hiding down arrow of Spinner by giving

android:background="@null"

to Spinner

So that item will take whole space of Spinner

Then put down arrow in a ImageView and align to right of Spinner

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:background="@null"
        android:layout_height="35dp"
        android:dropDownWidth="match_parent"
        android:spinnerMode="dropdown" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignEnd="@+id/spinner1"
            android:layout_alignRight="@+id/spinner1" 
            android:src="@drawable/arrow_down"
            android:layout_alignTop="@id/spinner1"
            android:layout_alignBottom="@+id/spinner1"/>
    </RelativeLayout>