Full-width Spinner in ActionBar

2019-01-30 05:12发布

问题:

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 "match_parent" in the Spinner layout resource, it doesn't fill the entire bar. Preferably, I'd like to be able to have it fill the entire bar except for my action items, rather than using the split actionbar as well.

EDIT: see my answer below for an implementation using the built-in actionbar, or hankystyles' when using a custom view

回答1:

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):



回答2:

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.



回答3:

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.



回答4:

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>