开发者

changing TextView properties of a ListView crashes the app

开发者 https://www.devze.com 2023-01-14 21:23 出处:网络
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Typeface font = Typeface.createFromAsset(
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Typeface font = Typeface.createFromAsset(
                getAssets(), "DejaVuSans.ttf");

        setListAdapt开发者_开发知识库er(new ArrayAdapter<String>(this, R.layout.list_item,
                        getGroups()));

        tv = (TextView)findViewById(R.id.textitem);

        //Uncomment a line below and the app crashes!!
        //tv.setTypeface(font);
        //tv.setGravity(Gravity.LEFT);

}

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textitem"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp"
    android:gravity="right"
/>

The problem is that I want to set a new typeface for the TextView in the ListView, and it seems it can't be done in the XML file!!


You can create the custom Adapter and can set the font face in that adapter. Here is one workable solution.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号