我为listView使用自定义适配器,我想用自定义字体制作textView。 listView是片段: todolist_fragment.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:id="@+id/checkable_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
todolistfragment.java
public class toDoListFragment扩展了Fragment {
ListView checkableList;
Button showSelectedButton;
List<itemsModel> itemsList;
public toDoListFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_todolist, container, false);
checkableList = rootView.findViewById(R.id.checkable_list);
itemsList = new ArrayList<>();
itemsList.add(new itemsModel(false, "item1"));
itemsList.add(new itemsModel(false, "item2"));
itemsList.add(new itemsModel(false, "item3"));
itemsList.add(new itemsModel(false, "item4"));
itemsList.add(new itemsModel(false, "item5"));
itemsList.add(new itemsModel(false, "item6"));
itemsList.add(new itemsModel(false, "item7"));
final customList adapter = new customList(getActivity(), itemsList);
checkableList.setAdapter(adapter);
checkableList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
itemsModel model = itemsList.get(i);
if(model.isSelected())
model.setSelected(false);
else
model.setSelected(true);
itemsList.set(i, model);
adapter.updateRecords(itemsList);
}
});
return rootView;
}
}
所以我创建了customList.java:
public class customList extends BaseAdapter {
Activity activity;
List<itemsModel> itemsList;
LayoutInflater inflater;
public customList(Activity activity) {
this.activity = activity;
}
public customList(Activity activity, List<itemsModel> itemsList) {
this.activity = activity;
this.itemsList = itemsList;
inflater = activity.getLayoutInflater();
}
@Override
public int getCount() {
return itemsList.size();
}
@Override
public Object getItem(int i) {
return i;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder = null;
if(view == null){
view = inflater.inflate(R.layout.list_view_item_row, viewGroup, false);
holder = new ViewHolder();
holder.itemText = view.findViewById(R.id.itemName);
holder.checkBox = view.findViewById(R.id.checkboxIcon);
view.setTag(holder);
}else
holder = (ViewHolder) view.getTag();
itemsModel model = itemsList.get(i);
holder.itemText.setText(model.getItemsList());
if(model.isSelected())
holder.checkBox.setBackgroundResource(R.drawable.checked);
else
holder.checkBox.setBackgroundResource(R.drawable.check);
return view;
}
public void updateRecords(List<itemsModel> itemsList){
this.itemsList = itemsList;
notifyDataSetChanged();
}
class ViewHolder{
TextView itemText;
ImageView checkBox;
}
}
和布局&#34; list_view_item_row.xml&#34;是使用imageview复选框和textview项目的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:padding="5dp" >
<ImageView
android:id="@+id/checkboxIcon"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:layout_centerVertical="true"
android:background="@drawable/check"/>
<TextView
android:id="@+id/itemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/checkboxIcon"
android:layout_toStartOf="@+id/checkboxIcon"
android:padding="10dp"
android:text="Item Name"
android:textColor="@android:color/black"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
/>
我手动设置自定义字体...像这样:
TextView textView;
textView = rootView.findViewById(R.id.text1);
Typeface customFontBold = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Assistant-Bold.ttf");
textView.setTypeface(customFontBold);
我可以在哪里设置自定义字体? 谢谢。
答案 0 :(得分:0)
在课堂上做出改变试试这个
public class customList extends BaseAdapter {
Activity activity;
List<itemsModel> itemsList;
LayoutInflater inflater;
private Typeface customFontBold;
public customList(Activity activity) {
this.activity = activity;
}
public customList(Activity activity, List<itemsModel> itemsList) {
this.activity = activity;
this.itemsList = itemsList;
inflater = activity.getLayoutInflater();
customFontBold = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Assistant-Bold.ttf");
}
@Override
public int getCount() {
return itemsList.size();
}
@Override
public Object getItem(int i) {
return i;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder = null;
if (view == null) {
view = inflater.inflate(R.layout.list_view_item_row, viewGroup, false);
holder = new ViewHolder();
holder.itemText = view.findViewById(R.id.itemName);
holder.checkBox = view.findViewById(R.id.checkboxIcon);
holder.itemText.setTypeface(customFontBold);
view.setTag(holder);
} else
holder = (ViewHolder) view.getTag();
itemsModel model = itemsList.get(i);
holder.itemText.setText(model.getItemsList());
if (model.isSelected())
holder.checkBox.setBackgroundResource(R.drawable.checked);
else
holder.checkBox.setBackgroundResource(R.drawable.check);
return view;
}
public void updateRecords(List<itemsModel> itemsList) {
this.itemsList = itemsList;
notifyDataSetChanged();
}
class ViewHolder {
TextView itemText;
ImageView checkBox;
}
}
答案 1 :(得分:0)
我使用了akhilesh0707答案,但我改变了它。
var data = new ObservableDataSource<Point>();
for (int i = 0; i < xvalues.Count(); i++)
{
data.Collection.Add(new Point(Convert.ToDouble(xvalues[i].Replace('.', ','), new CultureInfo("de-DE")) + xoffset, Convert.ToDouble(yaxes4Values[i].Replace('.', ','), new CultureInfo("de-DE"))));
}
LineGraph line = new LineGraph(data);
this.y2.Children.Add(line);
感谢akhilesh0707。