Full-width Spinner in ActionBar

前端 未结 4 1968
北海茫月
北海茫月 2020-12-23 02:34

I\'d really like my app to have a Spinner which stretches the entire length of my ActionBar, like the one in Gmail 4.0. Anyone know how to achieve this? Even if I set \"matc

相关标签:
4条回答
  • 2020-12-23 02:43

    The spinner on my gmail app also spans the entire width of my action bar. This solution worked for me:

    View spinner = getLayoutInflater().inflate(R.layout.actionbar_spinner, null);
    actionBar.setCustomView(spinner);
    actionBar.setDisplayShowCustomEnabled(true);
    

    Where the spinner layout is like so:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="fill_horizontal" >
        <Spinner 
            android:id="@+id/spinner"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:prompt="@string/spinner_text"
        />
    </RelativeLayout>
    

    I suspect there's a better solution to be had by overriding the default action bar navigation style like this but I couldn't immediately get that working.

    0 讨论(0)
  • 2020-12-23 02:50

    Bit annoying that I've just done it, but here is a method of doing it using the built-in Spinner in the action bar. All you need to do is make your spinner item's main container a RelativeLayout and set its gravity to fillHorizontal, like so:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="fill_horizontal"
    android:orientation="vertical" >
    
    <TextView
        android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="-4dip"
        android:text="@string/gen_placeholder"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    
    <TextView
        android:id="@+id/TextView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@android:id/text1"
        android:text="@string/app_name"
        android:textAppearance="?android:attr/textAppearanceSmall" />
    
    </RelativeLayout>
    

    And initialising the Adapter as so:

    ArrayAdapter<CharSequence> barAdapter = new ArrayAdapter<CharSequence>(this, R.layout.subtitled_spinner_item, 
        android.R.id.text1, getResources().getStringArray(R.array.actionList));
    barAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    

    which then gives the required spinner spanning the entire actionbar (except for the action buttons):

    Spanned actionbar

    0 讨论(0)
  • 2020-12-23 02:55

    Try setting the background of the spinner to an image file. The default background has caused me layout issues, but when I set it to my own png of a blank 1x1 pixle it filled my layout as I wanted. If that works you may have to create a custom image that works for you. I hope that helps.

    0 讨论(0)
  • 2020-12-23 03:08

    If you have a TextView inside your Spinner, you can set the minEms to a high value. This is the easiest workaround I explored:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_vertical">
    
        <TextView
            android:id="@+id/spinner_active"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:text="blaa"
            android:minEms="100"
            android:textSize="20sp" />
    
    </LinearLayout>
    
    0 讨论(0)
提交回复
热议问题