android - Android ListView 的自定义字体

标签 android listview fonts

我的 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/

相关文章:

android - 如何检测 Android 设备是以秒还是毫秒为单位存储媒体日期?

android - Android ICS 的 CSS 加载问题

c# - 如何在 ASP.NET ListView ItemTemplate 中返回 HTML?

java - Android ListView setOnItemClickListener 不工作

java - Android 抽屉布局显示在 fragment 内容下

css - 使用 css 嵌入字体

java - Android Studio 如何让通知出现在手机上?

android - 使用电子邮件和密码进行谷歌身份验证。

fonts - 如何在 Pango 和 Mono 中设置文件字体

java - 无法使用 iText 显示 unicode 0x0332