【发布时间】: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