格式化ListView android的“空列表”文本

时间:2013-04-12 19:10:39

标签: android listview

如何在Android ListView列表为空时格式化显示的文字?

<ListView
    android:id="@+id/android:list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:cacheColorHint="@android:color/transparent"
    android:dividerHeight="1dip" />

<TextView
    android:id="@+id/android:empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/test_list"
    android:textColor="@color/white"
    android:textSize="25sp"
     />

我想从资产中将字体设置为字体..

mTypeFace = Typeface.createFromAsset(context.getAssets(), "fonts/myFont.ttf");
mEmptyTextView = (TextView)findViewById(**????**)    
mEmptyTextView.setTypeface(mTypeFace);

2 个答案:

答案 0 :(得分:2)

您需要使用android.R.id.empty初始化mEmptyTextView。试试看:

mTypeFace = Typeface.createFromAsset(context.getAssets(), "fonts/myFont.ttf");
mEmptyTextView = (TextView)findViewById(android.R.id.empty);
mEmptyTextView.setTypeface(mTypeFace);

答案 1 :(得分:0)

<TextView
    android:id="@+id/empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/test_list"
    android:textColor="@color/white"
    android:textSize="25sp"/>

然后你可以通过id找到这个视图(不要使用android:前缀):

import <your.package>.R;
...
mEmptyTextView = (TextView)findViewById(R.id.empty);

ListView还有一个特殊的方法来设置空视图:

ListView#setEmptyView

http://developer.android.com/reference/android/widget/AdapterView.html#setEmptyView(android.view.View)

BTW你需要以某种方式隐藏空TextView(不在#setVisible(View.GONE)上调用TextView)将它放入某种容器中并隐藏容器 - 但这是一种不好的方法。

因此要么从不同的布局中膨胀TextView,要么在运行时创建它。