gpt4 book ai didi

Android ListView setTypeFace 和 setBackgroundDrawable

转载 作者:行者123 更新时间:2023-11-30 04:04:53 25 4
gpt4 key购买 nike

我有一个带有 OnItemClickListener 的 ListView,我在其中设置背景以指示该行已被选中

itemList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {

String item = (String) arg0.getItemAtPosition(position);
boolean itemSelected = selectedItems.contains(item);

if (!itemSelected) {
selectedItems.add(item);
v.setBackgroundDrawable(getResources().getDrawable(R.drawable.listitem_selected));
} else {
selectedItems.remove(item);
v.setBackgroundDrawable(null);
}
});

如果我为我的 ListView 使用默认适配器,ListView 行会正确突出显示

ArrayList<String> listOfItems = new ArrayList<String>();

....
final ArrayAdapter<String> itemAdapter = new ArrayAdapter<String>(this, R.layout.list_item, listOfItems);

如果我使用扩展 ArrayAdapter 的自定义适配器,以便我可以将 TextView 的字体设置为自定义字体,则字体设置正确但背景突出显示不再有效。有什么想法吗?

public class ListItemAdapter extends ArrayAdapter<String> {
private final Context context;
private final ArrayList<String> values;

public ListItemAdapter(Context context, ArrayList<String> values) {
super(context, R.layout.list_item, values);
this.context = context;
this.values = values;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.text1);

Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/font-file.otf");
textView.setTypeface(tf);
textView.setText(values.get(position));
return rowView;
}
}

最佳答案

试试这个。它对我有用。它也可能对您有帮助。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.text1);

Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/font-file.otf");
textView.setTypeface(tf);
textView.setText(values.get(position));

rowView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
arg0.setBackgroundResource(R.drawable.<background>);
}
});
return rowView;
}

关于Android ListView setTypeFace 和 setBackgroundDrawable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11850915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com