2011-12-15 71 views
5

我正在訪問Java文件中的微調器,將其輸入設置爲具有美國國家名稱的字符串數組。我想給我自己的大小在微調選擇狀態?如何給它定製尺寸?如何更改Android Spinner項目的文本大小

這是代碼:

Spinner state = (Spinner) findViewById(R.id.state); 
      ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
        this, R.array.state, android.R.layout.simple_spinner_item); 
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
      state.setAdapter(adapter); 

XML:

<Spinner 
android:id="@+id/state" 
android:layout_width="wrap_content" 
android:layout_height="60dip" 
android:layout_marginLeft="10dip" 
android:addStatesFromChildren="true" > 
</Spinner> 

字符串數組中的微調,以顯示:

<string-array name="state"> 

     <item>Select State</item> 
     <item>Alabama</item> 
     <item>Alaska</item> 
     <item>American Samoa</item> 
     <item>Arizona</item> 
     <item>Arkansas</item> 
     <item>California</item> 
     <item>Colorado</item> 
     <item>Connecticut</item> 
     <item>Delaware</item> 
     <item>District of Columbia</item> 
     <item>Florida</item> 
     <item>Georgia</item> 
     <item>Guam</item> 
     <item>Hawaii</item> 
     <item>Idaho</item> 
     <item>Illinois</item> 
     <item>Indiana</item> 
     <item>Iowa</item> 
     <item>Kansas</item> 
     <item>Kentucky</item> 
     <item>Louisiana</item> 
     <item>Maine</item> 
     <item>Maryland</item> 
     <item>Massachusetts</item> 
     <item>Michigan</item> 
     <item>Minnesota</item> 
     <item>Mississippi</item> 
     <item>Missouri</item> 
     <item>Montana</item> 
     <item>Nebraska</item> 
     <item>Nevada</item> 
     <item>New Hampshire</item> 
     <item>New Jersey</item> 
     <item>New Mexico</item> 
     <item>New York</item> 
     <item>North Carolina</item> 
     <item>North Dakota</item> 
     <item>Northern Marianas Islands</item> 
     <item>Ohio</item> 
     <item>Oklahoma</item> 
     <item>Oregon</item> 
     <item>Pennsylvania</item> 
     <item>Puerto Rico</item> 
     <item>Rhode Island</item> 
     <item>South Carolina</item> 
     <item>South Dakota</item> 
     <item>Tennessee</item> 
     <item>Texas</item> 
     <item>Utah</item> 
     <item>Vermont</item> 
     <item>Virginia</item> 
     <item>Virgin Islands</item> 
     <item>Washington</item> 
     <item>West Virginia</item> 
     <item>Wisconsin</item> 
     <item>Wyoming</item> 
    </string-array> 

回答

9

可以使用

adapter.setDropDownViewResource(android.R.layout.simple_list_item_1); 

或者你可以給它的佈局:

adapter.setDropDownViewResource(R.layout.my_layout); 

和你my_layout.xml應該是這樣的:

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+android:id/text1" android:layout_marginTop="5dp" 
     android:layout_marginBottom="5dp" android:padding="3dp" 
     android:layout_width="fill_parent" android:layout_height="wrap_content" 
     android:gravity="center_horizontal"/> 
2

嘗試使用自定義適配器這樣,

 ArrayAdapter<String> spinner_adapter = new ArrayAdapter<String>(getParent() 
       .getApplicationContext(), R.layout.my_spinner_style,spinner_values) { 

      public View getView(int position, View convertView, ViewGroup parent) { 
       View v = super.getView(position, convertView, parent); 
       ((TextView) v).setTextSize(18); 

       return v; 
      } 

      public View getDropDownView(int position, View convertView, 
        ViewGroup parent) { 
       View v = super.getDropDownView(position, convertView, parent); 
       v.setBackgroundResource(R.drawable.spinner_drop_down); 
       ((TextView) v).setTextColor(Color 
         .parseColor(config.sort_by_label_color)); 
       return v; 
      } 
     }; 

R.layout.my_spinner_style

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+android:id/text1" 
style="?android:attr/spinnerItemStyle" 
android:paddingLeft="5dip" 
android:singleLine="true" 
android:textColor="#ffffff" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:ellipsize="marquee" /> 

並將此適配器將微調,

 spinner.setAdapter(spinner_adapter); 
相關問題