Java customAdapter中的自定义字体
我为我的listView使用自定义适配器,我想用自定义字体创建textView。 listView位于片段中: todolist_fragment.xmlJava customAdapter中的自定义字体,java,android,listview,android-fragments,custom-adapter,Java,Android,Listview,Android Fragments,Custom Adapter,我为我的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:lay
<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"
/>
在哪里可以设置自定义字体?
谢谢。在课堂上做了一些改变。试试这个
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;
}
}
公共类customList扩展了BaseAdapter{
活动;
列表项目列表;
充气机;
私人字体;黑体字;
公共客户列表(活动){
这个。活动=活动;
}
公共自定义列表(活动,列表项列表){
这个。活动=活动;
this.itemsList=itemsList;
充气器=活动。GetLayoutFlater();
customFontBold=Typeface.createFromAsset(getActivity().getAssets(),“fonts/Assistant Bold.ttf”);
}
@凌驾
public int getCount(){
return itemsList.size();
}
@凌驾
公共对象getItem(int i){
返回i;
}
@凌驾
公共长getItemId(int i){
返回i;
}
@凌驾
公共视图getView(int i、视图视图、视图组视图组){
ViewHolder=null;
如果(视图==null){
视图=充气机。充气(R.layout.list\u view\u item\u row,viewGroup,false);
holder=新的ViewHolder();
holder.itemText=view.findviewbyd(R.id.itemName);
holder.checkBox=view.findViewById(R.id.checkboxIcon);
holder.itemText.setTypeface(customFontBold);
视图.设置标签(支架);
}否则
holder=(ViewHolder)view.getTag();
itemsModel模型=itemsList.get(i);
holder.itemText.setText(model.getItemsList());
if(model.isSelected())
holder.checkBox.setBackgroundResource(R.drawable.checked);
其他的
holder.checkBox.setBackgroundResource(R.drawable.check);
返回视图;
}
公共作废更新记录(列表项列表){
this.itemsList=itemsList;
notifyDataSetChanged();
}
类视图持有者{
文本查看项目文本;
ImageView复选框;
}
}
我使用了akhilesh0707答案,但我更改了它
public customList(Activity activity, List<itemsModel> itemsList) {
this.activity = activity;
this.itemsList = itemsList;
inflater = activity.getLayoutInflater();
customFontBold = Typeface.createFromAsset(activity.getApplication().getAssets(), "fonts/Assistant-Bold.ttf");
}
public customList(活动,列表项列表){
这个。活动=活动;
this.itemsList=itemsList;
充气器=活动。GetLayoutFlater();
customFontBold=Typeface.createFromAsset(activity.getApplication().getAssets(),“fonts/Assistant Bold.ttf”);
}
谢谢akhilesh0707。显然是在你给汽车充气的地方view@Selvin当然,我试过了,但是我不能使用“getActivity()”。然后呢?你所需要的只是背景。。。。语境无处不在。。。Fx检查查看文档。。。你们猜怎么着,你们可以从中获取上下文…你们可以在构造函数中传递上下文
<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);
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;
}
}
public customList(Activity activity, List<itemsModel> itemsList) {
this.activity = activity;
this.itemsList = itemsList;
inflater = activity.getLayoutInflater();
customFontBold = Typeface.createFromAsset(activity.getApplication().getAssets(), "fonts/Assistant-Bold.ttf");
}