2012-01-20 48 views
29

我真的很喜歡我的應用程序有一個Spinner,它延伸了我的ActionBar的整個長度,就像Gmail 4.0中的一個一樣。任何人都知道如何做到這一點?即使我在「微調」佈局資源中設置了「match_parent」,它也不會填滿整個欄。最好是,我希望能夠填充整個欄,除了我的動作項目,而不是使用分割動作欄。ActionBar中的全寬度微調器

編輯:使用內置的動作條,或hankystyles'使用自定義視圖時,看到我的回答如下用於實現

enter image description here

+0

如何填充 - 按鈕圖像資源本身是否包含一點填充? – Scalarr

+0

您是否設置了多個賬戶?礦山也像你一樣跨越右邊,但我認爲這主要是因爲我有幾個賬戶。 「下拉」(或稱之爲)的內容則不同 - 而且更廣泛。 – hnilsen

+1

是的,我有2個賬戶設置。取決於你擁有多少帳戶,它有所不同有點奇怪! –

回答

28

有點討厭我剛剛完成它,但是這裏有一個方法是使用操作欄中內置的Spinner進行操作。所有你需要做的是讓你的微調項的主要容器RelativeLayout及其嚴重性設置爲fillHorizo​​ntal,像這樣:

<?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> 

並初始化適配器像這樣:

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

,然後給需要的微調覆蓋了整個動作條(除了操作按鈕):

Spanned actionbar

+1

您可以通過設置微調器項目的「主容器」擴展您的意思嗎?你可以舉一個例子說明你如何添加上面的佈局? – brk3

+0

我的意思是你的微調項目的封閉佈局。上面的xml代碼生成全寬度微調器,如圖所示。 –

+0

那麼你需要一個自定義適配器來與此? – brk3

19

我的Gmail應用程序的微調也跨越了整個寬度我的行動欄。該解決方案爲我工作:

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

凡微調佈局就像這樣:

<?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> 

我懷疑有一個更好的解決方案,通過重寫默認的操作欄導航式like this可以了,但我不能」不要馬上得到那個工作。

+0

啊輝煌,謝謝。我更喜歡使用標準導航控件(即不是自定義視圖)的實現,但如果沒有人最終給我一個,我會獎勵賞金。 –

0

嘗試將微調框的背景設置爲圖像文件。默認的背景已經引起了我的佈局問題,但是當我將它設置爲我自己的1x1像素的PNG時,它會填充我的佈局。如果有效,您可能需要創建適合您的自定義圖像。我希望有所幫助。

0

如果你有一個TextVie在微調器內部,您可以將minEms設置爲較高值。這是我探索的最簡單的解決方法:

<?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>