我想将自定义字体应用于我的微调器。我发现的唯一方法是创建自定义适配器。这是我的代码
private class CustomAdapter extends ArrayAdapter {
private Context context;
private List<CharSequence> itemList;
public CustomAdapter(Context context, int textViewResourceId,List<CharSequence> itemList) {
super(context, textViewResourceId);
this.context=context;
this.itemList=itemList;
}
public TextView getView(int position, View convertView, ViewGroup parent) {
TextView v = (TextView) super
.getView(position, convertView, parent);
Typeface myTypeFace = Typeface.createFromAsset(context.getAssets(),
"fonts/gilsanslight.otf");
v.setTypeface(myTypeFace);
v.setText(itemList.get(position));
return v;
}
public TextView getDropDownView(int position, View convertView,
ViewGroup parent) {
TextView v = (TextView) super
.getView(position, convertView, parent);
Typeface myTypeFace = Typeface.createFromAsset(context.getAssets(),
"fonts/gilsanslight.otf");
v.setTypeface(myTypeFace);
v.setText(itemList.get(position));
return v;
}
}
然后我用
List<CharSequence> itemList = new ArrayList<CharSequence>(
Arrays.asList(items));
mySpinnerArrayAdapter = new CustomAdapter(context,android.R.layout.simple_spinner_item,itemList);
spinner.setAdapter(mySpinnerArrayAdapter);
执行此操作后,我的适配器为空。谁能帮帮我吗 ?这些项目包含国家/地区列表。
亲切的问候,
答案 0 :(得分:53)
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(yourRowlayout, parent,
false);
TextView make = (TextView) row.findViewById(R.id.Make);
Typeface myTypeFace = Typeface.createFromAsset(context.getAssets(),
"fonts/gilsanslight.otf");
v.setTypeface(myTypeFace);
v.setText(itemList.get(position));
return row;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(yourRowlayout, parent,
false);
TextView make = (TextView) row.findViewById(R.id.Make);
Typeface myTypeFace = Typeface.createFromAsset(context.getAssets(),
"fonts/gilsanslight.otf");
v.setTypeface(myTypeFace);
v.setText(itemList.get(position));
return row;
}
答案 1 :(得分:3)
试一试
在布局文件中:
<Spinner
android:id="@+id/spinnerview"
android:layout_width="180dp"
android:layout_height="42dp"
android:layout_marginLeft="105dp"
android:layout_marginTop="45dp"
android:background="@drawable/spinner_back"
android:paddingLeft="5dp"
android:spinnerMode="dropdown"
android:visibility="visible" />
在string.xml
:
<string-array name="spinner_array_environtment">
<item>Test</item>
<item>Production</item>
</string-array>
在onCreate()
方法:
spinner_environment = (Spinner) findViewById(R.id.spinnerview);
adapter = ArrayAdapter.createFromResource(this,
R.array.spinner_array_environtment, R.layout.spinner);
adapter.setDropDownViewResource(R.layout.spinner);
spinner_environment.setAdapter(adapter);
将新的spinner.xml
文件添加到布局文件夹中:
在spinner.xml
文件中:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinnerTarget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textColor="#4C4646" />
多数民众赞成!
答案 2 :(得分:3)
将itemList作为超类构造函数
中的参数传递public CustomAdapter(Context context, int textViewResourceId,List<CharSequence> itemList) {
super(context, textViewResourceId, itemList);
this.context=context;
this.itemList=itemList;
}
答案 3 :(得分:2)
最简单的,我认为:)
List<String> listOfItems = getListOfItems(); // returns ArrayList<String>
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listOfItems);
targetSpinner.setAdapter(spinnerAdapter);
好的,简单地在旋转器中放入一个字符串列表不应该强迫我们实现一个适配器。我认为,这是一个代码污点,并且有点疯狂。
Trick是simple_spinner_item id - 该死的,我喜欢R.id机制,但这并不是直接来自文档。
答案 4 :(得分:0)
这对我有用(使用android.R.layout。 simple_spinner_dropdown_item ):
@Override
public View getView(int position, View convertView, ViewGroup parent) {
CheckedTextView checkedTextView = (CheckedTextView) super.getView(position, convertView, parent);
checkedTextView.setText(itemList.get(position));
return checkedTextView;
}
我认为这是一个更好的解决方案,因为你不会多次膨胀。