2010-10-21 112 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图像。

祝你好运。