我想使用特定的textfont并实现以下类:
public class MyArrayAdapter extends ArrayAdapter<String> {
public MyArrayAdapter(Context context, int textViewResourceId, List<String> names) {
super(context, textViewResourceId, names);
}
public MyArrayAdapter(Context context, int layoutId, int textViewResourceId, List<String> names) {
super(context, layoutId, textViewResourceId, names);
}
public TextView getView(int position, View convertView, ViewGroup parent) {
TextView v = (TextView) super.getView(position, convertView, parent);
Typeface tf = Typeface.createFromAsset(
getContext().getAssets(),"fonts/calibri.ttf");
v.setTypeface(tf);
return v;
}
public TextView getDropDownView(int position, View convertView, ViewGroup parent) {
TextView v = (TextView)super.getDropDownView(position, convertView, parent);
Typeface tf = Typeface.createFromAsset(
getContext().getAssets(),"fonts/calibri.ttf");
v.setTypeface(tf);
return v;
}
}
它工作得很好(我使用了第一个构造函数),但后来我将布局从textView更改为包含textView的linearLayout。因此我使用了第二个构造函数。但后来我收到错误消息:
java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
我尝试使用原始的ArrayAdapter而不是我自定义的ArrayAdapter,当然,虽然我没有我的字体。我该如何使用这个?
答案 0 :(得分:1)
问题是
TextView v = (TextView) super.getView(position, convertView, parent);
和
TextView v = (TextView)super.getDropDownView(position, convertView, parent);
因为你假设返回的布局是TextView,这是错误的,因为它是一个LinearLayout,它不会从TextView继承,所以它不能被转换为它。
为了使这个工作,您可以尝试这样的事情:
public class MyArrayAdapter extends ArrayAdapter<String> {
private int mTextId = 0;
public MyArrayAdapter(Context context, int textViewResourceId, List<String> names) {
super(context, textViewResourceId, names);
mTextId = textViewResourceId;// remember it, so we can find the real TextView
}
public MyArrayAdapter(Context context, int layoutId, int textViewResourceId, List<String> names) {
super(context, layoutId, textViewResourceId, names);
mTextId = textViewResourceId;// remember it, so we can find the real TextView
}
public View getView(int position, View convertView, ViewGroup parent) {
View cell = super.getView(position, convertView, parent);
setFontForChild(cell);
return cell;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View cell = super.getDropDownView(position, convertView, parent);
setFontForChild(cell);
return cell;
}
private void setFontForChild(View layoutCell)
{
View realTextView = layoutCell.findViewById(mTextId);
if(realTextView instanceof TextView)
{
Typeface tf = Typeface.createFromAsset(
getContext().getAssets(),"fonts/calibri.ttf");
((TextView)realTextView).setTypeface(tf);
}
//else realTextView is null or is not a TextView.
}
}
修改强>:
查看ArrayAdapter source code,我发现问题是您的spinner_dropdown_item.xml
没有包含spinner_item_textView
ID的视图,因为两者都是spinner_item.xml
和spinner_dropdown_item.xml
被认为(由ArrayAdapter
)具有类似的结构。因此,为了解决您的问题,您有两个选择:
1)修改您的spinner_dropdown_item.xml
,以便它可以包含容器(LinearLayout
)和您的TextView作为孩子(不要忘记在{{1}上设置android:id="@+id/spinner_item_textView"
}})
2)修改您的spinner_item以仅使用CheckedTextView
(您可以使用FonTextView
设置图片)并为android:drawableLeft="@drawable/image1"
构造函数中的textViewResourceId
提供0。这是一种更有效的解决方案,因为它不需要实例化其他MyArrayAdapter
视图。
答案 1 :(得分:0)
我调用适配器的代码片段:
public void setupUnits(Set<UnitDescription> descriptions) {
List<String> names = new ArrayList<String>();
unitDescriptions = new UnitDescription[descriptions.size()];
unitDescriptions = descriptions.toArray(unitDescriptions);
for (UnitDescription unit : unitDescriptions) {
if (unit.getName()!=null) {
names.add(unit.getName());
}
}
if (names.isEmpty()) {
return;
}
MyArrayAdapter adp = new MyArrayAdapter(StartActivity.this,
R.layout.spinner_item, R.id.spinner_item_textView, names);
adp.setDropDownViewResource(R.layout.spinner_dropdown_item);
spinner.setAdapter(adp);
}
spinner_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/arrow"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="5dp"
android:contentDescription="@layout/spinner_item"
android:src="@drawable/image1" />
<de.schunk.schwenk.util.FontTextView
android:id = "@+id/spinner_item_textView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:singleLine="true"
android:textColor="@color/spinner_text" />
</LinearLayout>
spinner_dropdown_item.xml(sollpäterauchein LinearLayout werden,wennmöglich):
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textColor="@color/spinner_text" />