我正在访问java文件中的微调器,设置其输入是具有美国州名称的字符串数组。我想在旋转器中将自己的大小赋予选定的状态?如何给它定制尺寸?
这是代码:
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>
要在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>
答案 0 :(得分: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"/>
答案 1 :(得分: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);