【问题标题】:How to set custom font in ListView and AlertDialog?如何在 ListView 和 AlertDialog 中设置自定义字体?
【发布时间】:2020-01-08 22:14:53
【问题描述】:

我创建了 ListView,当我单击列表项时,它将打开带有 NegativeButton 的 AlertDialog 消息。这些工作完美。现在,我想将自定义字体设置为列表视图项和 AlertDialog 的标题、消息和 NegativeButton 的字体。即使我尝试了自定义字体库,但没有得到预期的输出。下面附上我尝试过的代码。 请问谁能告诉我怎么回事?

public class NewActivity extends AppCompatActivity {

ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new);

    Calligrapher calligrapher = new Calligrapher(this);
    calligrapher.setFont(this, "Acme-Regular.ttf", true);

    listView = findViewById(R.id.list1);
    final ArrayList<String> concept = new ArrayList<String>();
    concept.add("Who is the captain of Indian Cricket Team?");
    concept.add("When we are celebrating Teacher's Day?");
    concept.add("When we are celebrating Women's Day?");

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
android.R.layout.simple_list_item_1, concept);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @SuppressLint("ResourceType")
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            AlertDialog.Builder adb = new AlertDialog.Builder(NewActivity.this);
            switch (i)
            {

                case 0:
                    adb.setTitle(concept.get(i));
                    adb.setMessage("Virat Kholi");
                    adb.setNegativeButton("Close", null);
                    adb.show();
                    break;

                case 1:
                    adb.setTitle(concept.get(i));
                    adb.setMessage("September 5");
                    adb.setNegativeButton("Close", null);
                    adb.show();
                    break;

                case 2:
                    adb.setTitle(concept.get(i));
                    adb.setMessage("March 8");
                    Typeface customTypeOne = Typeface.createFromAsset(getAssets(), "Acme-Regular.ttf");
                    adb.setTypeface(customTypeOne);
                    adb.setNegativeButton("Close", null);
                    adb.show();
                    break;
   }
  }
 });
 }
}

xml文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NewActivity">

<ListView
    android:id="@+id/list1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="8dp"
    android:fontFamily="assets/Acme-Regular.ttf"
    tools:ignore="MissingConstraints" />


</android.support.constraint.ConstraintLayout>

【问题讨论】:

标签: java android xml android-listview android-alertdialog


【解决方案1】:

替换

 android:fontFamily="assets/Acme-Regular.ttf"

https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml

 android:fontFamily="@font/Acme-Regular.ttf"

【讨论】:

    【解决方案2】:

    您可以通过编程方式使用字体设置字体

        Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/font name.ttf");
    
         textview.setTypeface(custom_font);
    

    【讨论】:

      【解决方案3】:

      有两种方法可以更改 textview 字体系列

      xml文件中的第一个:

      默认情况下,android studio 字体保存在 res 的字体文件夹中。如果你想使用这些字体,请添加这一行 app:fontFamily="@font/alegreya_bold"

      在java中第二或在代码中实用

       TextView textView= view.findViewById(R.id.textview);
          textView.setTextColor(Color.BLACK);
          textView.setTextSize(13);
          Typeface avnier_roman = Typeface.createFromAsset(context.getAssets(), "AvenirLTStd-Roman.otf");
          textView.setTypeface(avnier_roman); 
      

      【讨论】:

        【解决方案4】:

        先生,您只是正确地使用了自定义字体名称的命名约定。因为android不允许资产/可绘制大写字母。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-04-10
          • 1970-01-01
          • 2011-04-15
          相关资源
          最近更新 更多