[英]How I can change the title of the spinner
如何更改微調器的標題(顏色,背景,文本大小...)。 我看過一些帖子,但沒有與我的應用相同的代碼。
main.class
public class cargarSpinner() {
listaSpinner = new ArrayList<SpinnerInfo>();
listaSpinner = cargarListSpinner();
SpinnerAdapter myAdapter = new SpinnerAdapter
(this,
android.R.layout.simple_spinner_item,
listaSpinner);
spinner.setPrompt("Title");
spinner.setAdapter(myAdapter);
}
spinner_row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fondoSpinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/spinner_naranja_gradiente"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/textSpinner"
style="@style/SpinnerText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/>
在XMl中使用:
android:prompt="@string/spinner_title
在Java代碼中:
spinner.setPrompt("Title")
您可以按照以下步驟使用自定義微調器適配器創建自定義微調器布局
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.travelreasons, R.layout.simple_spinner_item);
adapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
您的布局。simple_spinner_item
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="@style/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />
您的布局。simple_spinner_dropdown_item
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="@style/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="?android:attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee" />
對於樣式,請根據需要添加自定義尺寸和高度。
<style name="spinnerItemStyle" parent="android:Widget.TextView.SpinnerItem">
</style>
<style name="spinnerDropDownItemStyle" parent="android:TextAppearance.Widget.TextView.SpinnerItem">
</style>
嘗試這個:
Spannable wordtoSpan = new SpannableString("Title");
wordtoSpan.setSpan(new ForegroundColorSpan(Color.WHITE), 0,
wordtoSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
wordtoSpan.setSpan(new BackgroundColorSpan(Color.RED), 0,
wordtoSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spinner.setPrompt(wordtoSpan);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.