我有四个包含四个 ListView 的选项卡,我想为每个 ListView 设置一个背景,但每当我尝试添加背景时,它都会将图像放在 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 中添加背景,所以它会在 ListView 的每个单元格中添加图像,所以我尝试添加线性布局、 ListView 和 ImageView 并将那里有背景,但它强制关闭。我认为这是因为 tabhost 使用 main.xml 绘制主页并且它发生冲突,所以我什至尝试在其中添加 listview 仍然它 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));
}
最佳答案
好的,您的 XML 布局文件将在 setContentView()
方法中使用。您尚未发布包含 TabHost 的 Activity 代码,但我假设您使用的是默认的 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);
关于java - 为 ListView 设置背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3989268/