2013-04-12 47 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或在運行時創建他。