2013-04-12 48 views
1

如何在Android ListView中列表为空时显示文本的格式?为ListView设置“空列表”文本android

<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

,你将需要使用android.R.id.empty初始化mEmptyTextView。尝试,因为:

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

它会为ListFragment工作吗?在ListFragment的API 22的list_content.xml中,使用了@ + android:id/internalEmpty。 – sandrstar

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)

顺便说一句,你需要以某种方式隐藏空的TextView(没有呼吁#setVisible(View.GONE)TextView)将其放入某种容器并隐藏一个容器 - 但这是一个坏方法。

所以要么从不同的布局充气TextView或在运行时创建他。