android - 自定义字体未加载到对话框的 listView 中
问题描述
我在drawable文件夹下定义了字体文件夹和xml文件。我正在使用对话并为对话的外观定义了List_view.xml
and List_item.xml
;list_item.xml
但是,在出现对话框时未加载定义的自定义字体;显示默认的 android 字体。
我试图更改整个应用程序的默认字体,但对话仍会加载默认字体。
default-font-in-dialogue
我想在对话框中使用这个字体
public void showDialogListView(View view) {
dialog = new Dialog(personal_info_1.this);
dialog.setContentView(R.layout.list_view);
dialog.setTitle("Select Country");
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
//prepare a list view in dialog
listview_country = dialog.findViewById(R.id.dialogList);
ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), R.layout.list_item, R.id.txtitem, country_name);
listview_country.setAdapter(adapter);
listview_country.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
//Toast.makeText(personal_info_1.this, "Clicked Item: " + parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
textview_country_info.setText(parent.getItemAtPosition(position).toString());
dialog.dismiss();
}
});
dialog.show();
}
这里,country_name
数组适配器中的数组是从 onCreate 中的数据库方法中获取的。
list_item.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/txtitem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/quicksand_light"
android:padding="10dp"
android:text="Text"
android:textSize="16sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="@color/line_light"
/>
list_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/dialogList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/listview_background"></ListView>
</LinearLayout>
解决方案
您可以创建扩展 TextView 的 CustomTextView 类,并在 .xml 文件中使用该 CustomTextView 类,而不是简单的
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.TextView;
@SuppressLint("AppCompatCustomView")
public class CustomTextView extends TextView {
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
public CustomTextView(Context context) {
super(context);
if (!isInEditMode()) {
Typeface face = Typeface.createFromAsset(context.getAssets(),
"fonts/OpenSansSemiBold.ttf");
this.setTypeface(face);
}
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
if (!isInEditMode()) {
Typeface face = Typeface.createFromAsset(context.getAssets(),
"fonts/OpenSansSemiBold.ttf");
this.setTypeface(face);
}
}
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
if (!isInEditMode()) {
Typeface face = Typeface.createFromAsset(context.getAssets(),
"fonts/OpenSansSemiBold.ttf");
this.setTypeface(face);
}
}
}
在您的 .xml 中:
<app.com.packagename.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text"/>
推荐阅读
- mysql - 在基于另一个表的选择中创建动态列名
- php - 如何在laravel中计算excel文件导入中的行数?
- c# - StructureMap 注册单一组件类型,满足多于一种服务
- sql - 在“情况下”中使用列而不包括在 Group By 中 - 或使用分区
- javascript - Laravel 设计。如何显示检索到的标签
- python - 带有圣人(使用python)外壳的aws ec2上的“无法执行二进制文件”
- php - php is_integer 返回 true 到字符串值
- visual-studio-2019 - 如何自定义VS2019 v16生成的app_offline.htm文件
- javascript - 通过蓝牙连接设备
- excel - Excel VBA 仅从自定义函数设置默认值一次