How can I make the spinner width same as largest available item width

前端 未结 7 1562
天涯浪人
天涯浪人 2021-01-31 07:44

I realize even I use wrap_content for my spinner (I avoid using match_parent as I do not want the spinner overly long), and using match_parent

相关标签:
7条回答
  • 2021-01-31 07:53

    You should be able to get the effect you want by specifying the width of the dropdown as WRAP_CONTENT:

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:dropDownWidth="wrap_content"
        android:spinnerMode="dropdown" />
    

    This will measure up to 15 items in the spinner's adapter to determine the width it should use.

    0 讨论(0)
  • 2021-01-31 07:53

    You can try spinnerMode="dialog" but it is just a workaround, not the right answer. Because it will display a new window with a dialog.

    So here is the way do to it. It will automatically fit the content.

    It strongly depends how you set up your custom layout. May i suggest you to write your own xml file myspinner_custom_dropdown.xml. Recommanded way is now ConstraintLayout from androidX.

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <CheckedTextView
            android:id="@android:id/text1"
            style="?android:attr/spinnerDropDownItemStyle"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:ellipsize="marquee"
            android:singleLine="true"
            app:layout_constraintStart_toEndOf="@+id/item_image"
            app:layout_constraintTop_toTopOf="parent" />
    
        <ImageView
            android:id="@+id/item_image"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:src="@drawable/image"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    Important : you have yo use your custom layout with your adapter

    dataAdapter.setDropDownViewResource(R.layout.myspinner_custom_dropdown);
    

    That's all, you do not need some kinds of hardcored attributes in your spinner declaration.

     <Spinner
                android:id="@+id/my_spinner"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp" />
    
    0 讨论(0)
  • 2021-01-31 08:01

    Try this: android:layout_width="fill_parent"

    0 讨论(0)
  • 2021-01-31 08:03

    Put your TextView inside LineanLayout in Spinner Item XML,

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/text_view_0"
        style="?android:attr/spinnerItemStyle"
        android:singleLine="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawablePadding="10dp"
        android:gravity="center_vertical" />
    </LinearLayout>
    

    And Pass your TextView's id to custom adapter constructor

    CustomAdapter adapter = new CustomAdapter(this,R.layout.spinner_item,R.id.text_view_0,arraylist);
    

    Constructor

      public CustomAdapter(Context context, int resource, int textViewId, ArrayList<String> resultList) {
                    super(context, resource, textViewId, resultList);
            ...
            ...
                }
    
    0 讨论(0)
  • 2021-01-31 08:04

    Set your width to 0dp and a layout weight of 1. The following will resize it for you.

        <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="?android:attr/actionBarSize"
        android:gravity="center">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/test label"/>
    
        <Spinner
            android:id="@+id/test_spinner"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:dropDownWidth="wrap_content"
            android:layout_weight="1"
            android:prompt="@string/test_prompt"
            android:spinnerMode="dropdown" />
    </LinearLayout>
    
    0 讨论(0)
  • 2021-01-31 08:08

    I merely "solve" the problem by using

    <Spinner
        android:id="@+id/country_spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="200dp"
        android:layout_marginBottom="10dp" />
    

    200dp is just try-n-error value, to ensure all items in the drop down list are visible.

    0 讨论(0)
提交回复
热议问题