首页 > 解决方案 > 自定义字体未加载到对话框的 listView 中

问题描述

我在drawable文件夹下定义了字体文件夹和xml文件。我正在使用对话并为对话的外观定义了List_view.xmland List_item.xmllist_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>

标签: androidxmllistviewtextviewandroid-dialog

解决方案


您可以创建扩展 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"/>

推荐阅读