2012-12-19 54 views
9

我試圖得到一個ICS微調喜歡在我的應用程序,玩好幾個小時,最後我用HoloEverywhere得到這個,它的工作,但我有一個小disign問題,是因爲我在XML設置的微調並不包裹它的內容,並且在默認情況下是這樣的:Android的微調尺寸非常大

enter image description here

真的,我GOOGLE了好幾個小時了,而我發現的是,如何調整微調項目而不是視圖本身,就意味着我要微調調整到所選擇的項目大小是這樣的:

enter image description here

這裏是我的XML:

<RelativeLayout 
android:layout_width="match_parent" 
android:layout_height="40dp" 
android:orientation="horizontal" > 

<org.holoeverywhere.widget.Spinner 
    android:id="@+id/spnCities" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_centerVertical="true" 
    android:layout_marginRight="10dp" 
    /> 

<TextView 
    android:id="@+id/tvCities" 
    android:layout_width="70dp" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_centerVertical="true" 
    android:layout_marginLeft="10dp" 
    android:text="@string/city" 
    android:textAppearance="?android:attr/textAppearanceSmall" /> 

任何幫助將受到歡迎。

謝謝。

回答

6

微調具有最大寬度他的項目,但在大多數家長寬度(上layout_width = WRAP_CONTENT)。 您可以在org.holoeverywhere.widget包裝和覆蓋方法measureContentWidth創建CustomSpinner類:

@Override 
int measureContentWidth(SpinnerAdapter adapter, Drawable background) { 
    if (adapter == null) { 
     return 0; 
    } 
    View view = adapter.getView(getSelectedItemPosition(), null, this); 
    if (view.getLayoutParams() == null) { 
     view.setLayoutParams(new ViewGroup.LayoutParams(
       ViewGroup.LayoutParams.WRAP_CONTENT, 
       ViewGroup.LayoutParams.WRAP_CONTENT)); 
    } 
    view.measure(MeasureSpec.makeMeasureSpec(0, 
      MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, 
      MeasureSpec.UNSPECIFIED)); 
    int width = view.getMeasuredWidth(); 
    if (background != null) { 
     Rect mTempRect = new Rect(); 
     background.getPadding(mTempRect); 
     width += mTempRect.left + mTempRect.right; 
    } 
    return width; 
} 
+0

這也爲我工作! 創建自己的CustomSpinner,只是implemeting該方法(無覆蓋),並使用IR在你的應用程序中所選項目的寬度剛好有他的真實與:) 在以前的版本中飛旋在默認情況下做到這一點,至少2.3 – Rako