gpt4 book ai didi

android - Android ListView 的自定义字体

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:46:25 29 4
gpt4 key购买 nike

我的 android Activity 有一个自定义字体。

MainActivity.class

    private void initControls() {
// TODO Auto-generated method stub
header = (TextView) findViewById (R.id.tvAccommodations);
lv = (ListView) findViewById (R.id.lvAccommodations);
text = (TextView) findViewById (R.id.textView);

Typeface tf = Typeface.createFromAsset(getAssets(),
"fonts/heartbre.ttf");
header.setTypeface(tf);
text.setTypeface(tf);



ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.abra_hotel, R.layout.custom_list_text);
lv.setAdapter(adapter);
header.setText(value);

custom_list_text.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:text="@string/app_name"
android:paddingLeft="6dip" />

Android 抛出 NullPointerException。为什么会这样?任何帮助表示赞赏。谢谢。

LOGCAT:

    03-08 19:48:03.859: E/AndroidRuntime(413): Caused by: java.lang.NullPointerException
03-08 19:48:03.859: E/AndroidRuntime(413): at com.say.philippineexplorer.PlaceAccommodations.initControls(PlaceAccommodations.java:34)
03-08 19:48:03.859: E/AndroidRuntime(413): at com.say.philippineexplorer.PlaceAccommodations.onCreate(PlaceAccommodations.java:22)

最佳答案

这是自定义适配器类和构造函数

class CustomAdapter extends ArrayAdapter<CharSequence>{

Context context;
int layoutResourceId;
CharSequence data[] = null;
Typeface tf;

public CustomAdapter(Context context, int layoutResourceId, CharSequence[] data, String FONT ) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
tf = Typeface.createFromAsset(context.getAssets(), FONT);
}

将你想要使用的字体放在你的 Assets 文件夹中,并像这样填充你的 ListView :

listAdapter = new CustomAdapter(this, R.layout.custom_list_text, R.array.abra_hotel, "name_of_font.ttf");

关于android - Android ListView 的自定义字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15293437/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com