繁体   English   中英

如何在ListView和AlertDialog中设置自定义字体?

[英]How to set custom font in ListView and AlertDialog?

我创建了ListView,当我单击列表项时,它将使用NegativeButton打开AlertDialog消息。 这些工作完美。 现在,我想将自定义字体设置为listview项,并设置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>

更换

 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"

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

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

     textview.setTypeface(custom_font);

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

首先在xml文件中:

默认情况下,android studio字体保存在res的字体文件夹中。 如果要使用这些字体,请添加以下行应用程序: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); 

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM