2010-10-21 167 views
1

我有舉辦四名列表視圖四個選項卡,我要爲每個列表視圖中的背景,但每當我嘗試添加背景,然後將圖像在ListView的每個細胞,而不是背後的名單。設置背景的列表視圖

<?xml version="1.0" encoding="utf-8"?> 

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 

android:background="@drawable/pre" 

android:layout_width="fill_parent" 
android:layout_height="fill_parent" 

android:padding="10dp" 

android:textSize="21sp"> 

</TextView> 

我意識到,這是怎麼一回事,因爲我已經嘗試添加背景在一個TextView所以它添加在列表視圖每個單元格的形象,所以我試圖添加的LinearLayout,ListView和一個imageview並放置背景,但強制關閉。我認爲這是tabhost使用main.xml來繪製主頁和它衝突,所以我甚至試圖添加列表視圖仍然nforce關閉,它只會工作,如果我只有一個textview,我可以添加一個每個listview的背景,下面是listview代碼;

public class prem extends ListActivity { 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    // Create an array of Strings, that will be put to our ListActivity 
    String[] names = new String[] { "Pre"}; 
    ListView lv = getListView();  
    lv.setCacheColorHint(00000000);  
    lv.setAdapter(new ArrayAdapter<String>(this, 
      R.layout.list_item, names)); 

} 

回答

3

好的,您的XML佈局文件將用於setContentView()方法。您尚未發佈包含TabHost的活動代碼,但我假設您使用的是默認的setContentView(R.layout.main);。如果你不使用setContentView()(在ListActivity的情況下),加入一個ListView到XML文件是不會改變任何東西,因爲它從來沒有被使用。

你在你所遇到的問題,因爲你設置的TextView的背景正確。由於您使用的是ListActivity,因此您需要使用代碼設置ListView的背景。 ListView是View的一個子類,因此您可以使用methods from the View class爲ListView設置背景資源。

例如:

ListView listView = getListView(); 

//set background to color 
listView.setBackgroundColor(#FF888888); 

//set background to Drawable 
listView.setBackgroundDrawable(myDrawable); 

//set background to Resource 
listView.setBackgroundResouce(R.id.my_res_id); 
0

按JonniBravo,加入....

如果您正在使用設置使用的setContentView視圖的XML佈局文件()在您的活動,你可以在裏面定義一個ListView。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/lists_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@drawable/paper_frame" /> 
</RelativeLayout> 

活性可以「找到它」使用

lv = getListView(); 

作爲示例所示以上,可以設置用於與「背景」屬性ListView中的背景。像往常一樣,這可以是一個有效的可繪圖(例如顏色或圖像)。在我的例子它正好是由拉伸的Android以包圍列表中,提供了一個幀一個9.patch圖像。

祝你好運。