设置背景的列表视图 [英] set a background for a listview

查看:104
本文介绍了设置背景的列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有四个选项卡,举办四场列表视图,我想设置一个背景为每个列表视图,但每当我尝试添加背景它把图像列表视图的每个细胞中,而不是列表后面。

 < XML版本=1.0编码=UTF-8&GT?;

< TextView中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android

机器人:背景=@可绘制/ pre

机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT

机器人:填充=10dp

机器人:TEXTSIZE =21sp>

< / TextView的>
 

我才知道这是怎么一回事,因为我已经尝试添加背景一个TextView所以它添加在列表视图每个单元中的形象,所以我试图添加的LinearLayout,ListView和一个ImageView的,把背景有,但它的力量关闭。我认为这是becuse的tabhost使用main.xml中绘制的主网页,并发生冲突,所以我甚至尝试添加列表视图仍然有它的nForce关闭,它只会工作,如果我只有一个TextView,豪我可以加背景每​​个列表视图,下面是列表视图code;

 公共类$ P $点扩展ListActivity {

/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包冰柱){
    super.onCreate(冰柱);
    //创建一个字符串数组,将被投入到我们的ListActivity
    的String []的名称=新的String [] {pre};
    ListView的LV = getListView();
    lv.setCacheColorHint(00000000);
    lv.setAdapter(新ArrayAdapter<字符串>(这一点,
            R.layout.list_item,名称));

}
 

解决方案

好了,你的XML布局文件会在的setContentView()方法中使用。您还没有贴code为您的活动,其中包括了TabHost,但我会假设你使用的是默认的setContentView(R.layout.main); 。如果你不使用的setContentView()(在ListActivity的情况下),添加一个ListView到XML文件是不会改变任何东西,因为它永远不会被使用。

您在您所遇到的问题,因为你设置的TextView的背景是正确的。由于您使用的是ListActivity,你需要使用code设置ListView的背景。 ListView控件是View的子类,所以你可以使用从View类方法一>设置你的背景资源为ListView。

例如:

  ListView控件的ListView = getListView();

//设置背景颜色
listView.setBackgroundColor(#FF888888);

//设置背景绘制对象
listView.setBackgroundDrawable(myDrawable);

//设置背景资源
listView.setBackgroundResouce(R.id.my_res_id);
 

I have four tabs that hold four listviews, I want to set a background for each list view but whenever I try to add the background it puts the image in each cell of the listview instead of behind the list.

<?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>

I've realised that this is beacuse I've tried to add the background in a textview so it adds the image in each cell in the listview, so I have tried to add a linearlayout, listview and a imageview and put the background there but it force closes. I think this is becuse the tabhost uses main.xml to draw the main page and it conflicts, so I even tried to add the listview there still it nforce closes, it will only work if I have a textview only, howe can I add a background to each listview, below is the listview code;

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));

}

解决方案

Okay, your XML layout files are going to be used in the setContentView() method. You haven't posted the code for your activity which includes the TabHost, but I'll assume you're using the default setContentView(R.layout.main);. If you're NOT using setContentView() (in the case of your ListActivity), adding a ListView to an XML file isn't going to change anything, because it's never being used.

You are correct in that you're having the issue because you're setting the background of the TextView. Since you're using a ListActivity, you'll need to set the ListView's background using code. ListView is a subclass of View, so you can use the methods from the View class to set your background resource for the ListView.

For example:

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);

这篇关于设置背景的列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆