我有5种字体样式(Typeface
),并希望向用户ListView
用不同的字体样式显示5次。
下面是我尝试过的代码。
public void ShowTextStyles(String inputText, int color){
final Dialog dialog = new Dialog(EditImageActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.textstylelayout);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
// Array of strings...
final String[] textStyles={"Gobold Bold.ttf","beyond_wonderland.ttf"};
final String[] mobileArray = {inputText,inputText,inputText,inputText,inputText,inputText,inputText,inputText,inputText,inputText};
ArrayAdapter adapter = new ArrayAdapter<String>(EditImageActivity.this,
R.layout.text_style_list, mobileArray);
ListView listView = (ListView) dialog.findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
// Set an item click listener for ListView
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Get the selected item text from ListView
String selectedItem = (String) parent.getItemAtPosition(position);
// Display the selected item text on TextView
}
});
dialog.setCancelable(true);
dialog.show();
}
答案 0 :(得分:1)
为此,您需要使用BaseAdapter
public class Myadapter extends BaseAdapter {
AssetManager assetManager = getAssets();
LayoutInflater lif;
ImageView sideArrow;
TextView tv;
public Myadapter(Context ctx) {
lif = (LayoutInflater) ctx.getSystemService(LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return favarets.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = lif.inflate(R.layout.inflate, null);
sideArrow = (ImageView) vi.findViewById(R.id.imageViewsidemark);
tv = (TextView) vi.findViewById(R.id.textFav);
tv.setText(favarets.get(position));
final Typeface tvFont = Typeface.createFromAsset(assetManager, "OPTIMA.TTF");
tv.setTypeface(tvFont);
tv.setTextColor(Color.BLACK);
return vi;
}
}
这是示例如何使用customAdapter https://guides.codepath.com/android/Using-a-BaseAdapter-with-ListView
解决方案2:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, filled_arr) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text = (TextView) view.findViewById(android.R.id.text1);
text.setTypeface(typeface);
return view;
}
};
答案 1 :(得分:1)
创建自定义适配器并将其用于您的列表视图:
public class TestAdapter extends BaseAdapter {
private Context context;
private List<String> appData;
String[] fontFiles = {"Gobold Bold.ttf", "beyond_wonderland.ttf"}; // add more fonts if wish more styles
public TestAdapter(Context context, List<String> appData) {
this.context = context;
this.appData = appData;
}
@Override
public int getCount() {
return appData.size();
}
@Override
public Object getItem(int position) {
return appData.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
private class ViewHolder {
TextView tv_item;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_app, null);
holder = new ViewHolder();
holder.tv_item = convertView.findViewById(R.id.tv_item);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv_item.setText(appData.get(position));
Typeface typeface;
// change this condition according to your need
if (position == 0){
typeface = Typeface.createFromAsset(context.getAssets(), fontFiles[0]);
}else {
typeface = Typeface.createFromAsset(context.getAssets(), fontFiles[1]);
}
holder.tv_item.setTypeface(typeface);
return convertView;
}
}