自定义字体为Android列表视图 [英] Custom font for Android listview

查看:116
本文介绍了自定义字体为Android列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义字体为我的Andr​​oid活动。

MainActivity.class

 私人无效initControls(){
    // TODO自动生成方法存根
    标题=(TextView中)findViewById(R.id.tvAccommodations);
    LV =(ListView控件)findViewById(R.id.lvAccommodations);
    文=(TextView的)findViewById(R.id.textView);

    字样TF = Typeface.createFromAsset(getAssets()
            字体/ heartbre.ttf);
    header.setTypeface(TF);
    text.setTypeface(TF);



    ArrayAdapter< CharSequence的>适配器= ArrayAdapter.createFromResource(这一点,
                    R.array.abra_hotel,R.layout.custom_list_text);
            lv.setAdapter(适配器);
            header.setText(值);
 

custom_list_text.xml

 < TextView中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@ + ID / list_text
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:TEXTSIZE =18sp
机器人:TEXTSTYLE =黑体
机器人:文本=@字符串/ APP_NAME
机器人:以下属性来=6dip/>
 

安卓引发NullPointerException异常。为什么会这样呢?任何帮助是AP preciated。谢谢你。

logcat的:

  03-08 19:48:03.859:E / AndroidRuntime(413):由:显示java.lang.NullPointerException
    03-08 19:48:03.859:E / AndroidRuntime(413):在com.say.philippineexplorer.PlaceAccommodations.initControls(PlaceAccommodations.java:34)
    03-08 19:48:03.859:E / AndroidRuntime(413):在com.say.philippineexplorer.PlaceAccommodations.onCreate(PlaceAccommodations.java:22)
 

解决方案

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

 类CustomAdapter扩展ArrayAdapter< CharSequence的> {

    上下文语境;
    INT layoutResourceId;
    CharSequence的数据[] = NULL;
    字样TF;

公共CustomAdapter(上下文的背景下,INT layoutResourceId,为CharSequence []的数据,字符串FONT){
    超级(上下文,layoutResourceId,数据);
    this.layoutResourceId = layoutResourceId;
    this.context =背景;
    this.data =数据;
    TF = Typeface.createFromAsset(context.getAssets(),字体);
}
 

将要在资产使用的文件夹,并填写您的ListView像这样的字体:

  listAdapter =新CustomAdapter(这一点,R.layout.custom_list_text,R.array.abra_hotel,name_of_font.ttf);
 

I have a custom font for my 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 throws NullPointerException. Why is it so? Any help is appreciated. Thanks.

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)

解决方案

Here is the custom adapter class and the constructor

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);
}   

Put the font you want to use in your assets folder and fill your listview like this:

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

这篇关于自定义字体为Android列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆