自定义字体未加载到对话框的 listView 中

     2023-04-19     116

关键词:

【中文标题】自定义字体未加载到对话框的 listView 中【英文标题】:custom font are not loaded in dialog's listView 【发布时间】:2019-06-05 06:37:01 【问题描述】:

我已经在drawable文件夹下定义了字体文件夹和xml文件。我正在使用对话并定义了List_view.xmlList_item.xml 以显示对话;然而,list_item.xml 中定义的自定义字体在对话框出现时没有加载;显示默认的 android 字体。

我已尝试更改整个应用的默认字体,但对话仍会加载默认字体。

default-font-in-dialoguei want to use this font in dialog



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_
android:layout_
android:orientation="vertical">

<TextView
    android:id="@+id/txtitem"
    android:layout_
    android:layout_
    android:fontFamily="@font/quicksand_light"
    android:padding="10dp"
    android:text="Text"
    android:textSize="16sp" />

<View
    android:layout_
    android:layout_
    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_
    android:layout_
    android:orientation="vertical">

    <ListView
        android:id="@+id/dialogList"
        android:layout_
        android:layout_
        android:background="@drawable/listview_background"></ListView>

</LinearLayout>

【问题讨论】:

【参考方案1】:

我在这里发布的最简单的方法之一,请看一下。

您只需在“list_item.xml 文件”的 textview 中删除“android:fontfamily”元素 然后执行以下操作。

1.在res/font/中创建一个字体文件夹然后 2.您只需在 res/values/style.xml 中提及如下-(只需将字体系列项添加到资源中)

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="android:fontFamily">@font/ubuntu_regular</item>
    <item name="fontFamily">@font/ubuntu_regular</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="android:fontFamily">@font/ubuntu_regular</item>
    <item name="fontFamily">@font/ubuntu_regular</item>
</style>

3.然后您只需运行代码,这种基于 style.xml 的字体将自动应用于整个应用程序。您无需在应用程序的任何位置添加字体系列。

【讨论】:

你添加你的字体文件而不是“font/ubuntu_regular”,比如“@font/ 我已经按照你说的尝试了,但是没有用。还是一样的问题。 @Rajaji Subramanian 您是否从“list_item.xml”中删除了这一行“android:fontFamily="@font/quicksand_light"? 请仔细检查一次,因为这是我发布的 100% 有效的解决方案。 如果您按照我发布的方式进行操作,则无需在应用程序的任何位置提供字体。即使是自定义字体字体也会更改默认警报对话框文本中的字体【参考方案2】:

您可以创建扩展 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_
android:layout_            
android:text="text"/>

【讨论】:

不客气。如果你能接受这个答案会很高兴。

自定义对话框中不可能有 Listview 吗?

】自定义对话框中不可能有Listview吗?【英文标题】:IsitimpossibletohaveListvVewinCustomDialog?【发布时间】:2021-01-0715:05:51【问题描述】:如果代码中没有调用showChatList()函数,则对话框正常显示。当listView通过showChatList()函数调用时,... 查看详情

加载资源失败:404(未找到)- Rails 自定义字体

】加载资源失败:404(未找到)-Rails自定义字体【英文标题】:Failedtoloadresource:404(NotFound)-Railscustomfonts【发布时间】:2014-09-1610:00:51【问题描述】:我正在尝试通过CSS@font-face加载一些自定义字体。我正在使用railsfonts_path方法。... 查看详情

自定义标签未加载到模板中

】自定义标签未加载到模板中【英文标题】:Customtagnotloadedintemplate【发布时间】:2016-05-0509:36:21【问题描述】:我创建了一个我想使用的自定义标签,但Django似乎找不到它。我的templatetags目录是这样设置的:pygmentize.pyfrompygmentsi... 查看详情

将自定义字体加载到网站中[重复]

】将自定义字体加载到网站中[重复]【英文标题】:Loadingcustomfontsintothewebsite[duplicate]【发布时间】:2015-07-2911:57:30【问题描述】:任何人都可以帮助我,我在我的网站项目中使用了特定的字体。匿名用户不知道如何下载、安装这... 查看详情

自定义 SVG 未加载到我的图像标签中

】自定义SVG未加载到我的图像标签中【英文标题】:CustomSVGisnotloadinginmyimagetag【发布时间】:2021-10-2809:58:10【问题描述】:目前我正在尝试在图像标签内加载我的自定义svg组件。但由于某种原因,我无法看到地图组件内的svg图像... 查看详情

自定义 Django 模板标签未加载到模板中

】自定义Django模板标签未加载到模板中【英文标题】:CustomDjangoTemplateTagsnotbeingloadedintoTemplates【发布时间】:2017-07-2913:27:21【问题描述】:我已经在互联网上搜索了很长一段时间,但无法解决我的问题。我正在尝试在我的一个Dja... 查看详情

自定义列表视图未出现在对话框中

】自定义列表视图未出现在对话框中【英文标题】:CustomListviewdoesn\'tappearinDialog【发布时间】:2021-04-1010:32:33【问题描述】:新年快乐!我从Android开始,我想在对话框中显示自定义列表视图,但是当我单击生成对话框的按钮时... 查看详情

如何从解析获取列表视图到自定义对话框?

】如何从解析获取列表视图到自定义对话框?【英文标题】:Howtogetlistviewfromparsetoacustomdialog?【发布时间】:2015-10-1419:21:55【问题描述】:我正在创建一个对话框,其中显示了可供选择的配置文件列表。我这样做的方式是使用列... 查看详情

无法将自定义字体加载到 OSX 项目中,但是......我进入了 iOS 项目?

】无法将自定义字体加载到OSX项目中,但是......我进入了iOS项目?【英文标题】:NotabletoloadacustomfontintoanOSXprojectbut...amintoaniOSproject?【发布时间】:2016-02-1319:13:46【问题描述】:我能够在iOS项目中加载自定义字体并使用以下方法... 查看详情

如何配置 webpack 以将自定义字体加载到故事书中?

】如何配置webpack以将自定义字体加载到故事书中?【英文标题】:Howtoconfigwebpacktoloadcustomfontsintostorybook?【发布时间】:2020-05-2720:34:16【问题描述】:我是webpack的新手,我正在尝试按照本教程https://medium.com/@mushti_dev/hey-e6faa20b910a... 查看详情

使用自定义 CursorAdapter 时 ListView 未显示在活动中

】使用自定义CursorAdapter时ListView未显示在活动中【英文标题】:ListViewnotshowingupinactivitywhenusingcustomCursorAdapter【发布时间】:2017-05-1319:20:24【问题描述】:我正在练习SQLiteDatabases。我做了两个活动MainActivity和EditorActivity。我使用带... 查看详情

自定义字体未显示在字体系列列表中

】自定义字体未显示在字体系列列表中【英文标题】:CustomFontsnotshowingonfontfamilylist【发布时间】:2020-06-1014:16:10【问题描述】:我在macOSCatalina上使用Swift5和Xcode11.4.1。直到今天,我在应用自定义字体时都没有遇到任何问题。我... 查看详情

ListView 从 EditText 更新

...808:06:19【问题描述】:我有一个自定义列表视图,它是从对话框中的EditText组件更新的。我有自定义行、适配器类和自定义对话框都可以工作,但我似乎无法触发adatper类中的代码,该代码会将文本从编辑文本控件添加到列表中。... 查看详情

listview 项目未在自定义适配器的 getview 方法中显示分配的值

】listview项目未在自定义适配器的getview方法中显示分配的值【英文标题】:listviewitemisnotdisplayingtheassignedvalueingetviewmethodofcustomadapter【发布时间】:2021-04-1920:27:55【问题描述】:我遇到了一个问题,但仍然不知道如何摆脱。我为... 查看详情

覆盖自定义 Visual Studio 编辑器中的字体

...义编辑器看起来与当前主题所指示的不同。编辑器托管在对话框中,并且应该具有托管对话框定义的相同字体。编辑器的内容类型定义如下:[Export][Name("MyContent")][BaseDefinitio 查看详情

带有复选框的自定义 ListView 检查未选中的项目

】带有复选框的自定义ListView检查未选中的项目【英文标题】:customListViewwithcheckboxescheckinguncheckeditems【发布时间】:2013-09-0216:49:06【问题描述】:我的ListView有一个BaseAdapter,位于片段中,如下所示:publicclassSelectionMucListAdapterext... 查看详情

属性标题未在 xib 文件的 UIButton 中加载自定义字体

】属性标题未在xib文件的UIButton中加载自定义字体【英文标题】:attributedtitlenotloadingcustomfontinUIButtoninxibfile【发布时间】:2015-05-0820:46:14【问题描述】:我正在努力解决这个问题,但不知道出了什么问题。我的一个界面构建器xib... 查看详情

数据未从 SQLite 加载到 Listview

】数据未从SQLite加载到Listview【英文标题】:DataisnotloadedintoaListviewfromSQLite【发布时间】:2019-07-1921:19:31【问题描述】:我正在使用androidstudio创建一个简单的crud系统,但记录已成功添加到数据库中。但是当我尝试在listview控件视... 查看详情