Android Spinner: Remove Extra White Space Between Text and Dropdown Icon

旧城冷巷雨未停 提交于 2019-12-19 07:41:46

问题


I have a spinner and by default there is extra white-space between text and dropdown icon which I really don't like and wanna remove it.

Tried searching over the web but did not get anything which could help me. Anybody here who has done it earlier?


回答1:


I did it myself after playing with Spinner. Here is the solution which worked pretty well.

First create a Dropdown with indicator image of your choice.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape
        android:shape="rectangle"/>
</item>
<item
    android:width="24dp"
    android:height="24dp"
    android:gravity="right|center_vertical">
    <bitmap
        android:src="@drawable/ic_dropdown"
        android:tint="@color/colorPrimaryDark"/>
</item></layer-list>

Then assign it to the background of Spinner.

<Spinner
        android:id="@+id/basket_spinner_item_quantity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:spinnerMode="dropdown"
        android:padding="0dp"
        android:background="@drawable/ic_spinner_dropdown"/>

Now, adjust the padding and alignment of spinner item as per your requirement.

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/simple_spinner_text_quantity"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="20dp"
android:paddingRight="25dp"
android:gravity="right"
android:textAlignment="gravity"/>



回答2:


Simply just add the following attribute to your spinner xml element:

For API < 17:

android:gravity="right"

For API => 17:

android:gravity="end"



回答3:


Quickfix

android:layout_marginStart="-40dp"

play with negative margin, according to need. inside you spinner item xml




回答4:


For some reason giving padding:20dp solved the problem for me. Also, play with the values of translationX to adjust the position of the dropdown icon. At last, you can use layout_marginLeft and layout_marginRight to move the whole dropdown accordingly.




回答5:


Use below code for Quick fixed

 <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="-9dp"
        android:layout_marginTop="10dp"
        android:entries="@array/almanac"
        android:gravity="left"
        android:padding="0dp"
        android:spinnerMode="dropdown"
        android:layout_marginStart="-9dp" />



回答6:


Remove padding of spinner in xml

<Spinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:padding="0dp"     
    android:id="@+id/my_spinner">

SpinnerAdapter or your Adapter class should be customized according to fit your needs.

SpinnerAdapter.getView(position, convertView, parent)    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        view.setPadding(0, view.getPaddingTop(), view.getPaddingRight(), view.getPaddingBottom());
        return view;
}

Change style attribute in your spinner list item layout



来源:https://stackoverflow.com/questions/45845453/android-spinner-remove-extra-white-space-between-text-and-dropdown-icon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!