无法将背景设置为ListView

时间:2011-09-21 05:49:37

标签: image listview android-layout background

我正在尝试为列表视图设置背景。我从drawable中获取了一个图像,并将其设置为布局文件的背景,然后将结果显示为以下屏幕截图screen with background

当我刚刚删除背景时,我得到了我需要但没有背景的东西。 和屏幕截图是screen without background 是否有任何其他语法为ListView设置背景。

我的布局代码是

    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"

      >


    <TextView android:text="@+id/TextView01" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:id="@+id/label"
            android:textColor="#FF0000"
            android:textSize="30px"></TextView>
      </LinearLayout>




And my java code and that is 

String[] names = new String[] { "India", "Malaysia" };
        // Use your own layout and point the adapter to the UI elements which
        // contains the label
        this.setListAdapter(new ArrayAdapter<String>(this, R.layout.screen3,
                R.id.label, names));
    }

    @Override

    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        // Get the item that was clicked
        Object o = this.getListAdapter().getItem(position);
        String keyword = o.toString();
        if(keyword.equals("India"))
        {

            Intent ima1=new Intent(screen3.this,new21.class);

            startActivity(ima1);

        }
        else
        {
            Intent ima2=new Intent(screen3.this,new22.class);
            startActivity(ima2);
        }

        Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG)
                .show();

    }

最后我需要的是我希望背景图像与正确的列表视图一个接一个地显示一个列表项。提前致谢     }

2 个答案:

答案 0 :(得分:0)

尝试此代码,您可以设置包含列表视图的布局和列表视图内容的单独布局,而不是扩展ListActivity。

Java代码:

public class Table_Layout extends Activity {

  ListView list_view;
  @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.table_layout);
      list_view=(ListView)findViewById(R.id.list_view);
      String[] names = new String[] { "India", "Malaysia" };
      list_view.setAdapter(new ArrayAdapter<String>(this, R.layout.screen3,R.id.label, names));
    }
}

包含列表视图的主XML:table_layout.xml

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" android:background="@drawable/uploading">

    <ListView   android:layout_width="fill_parent" android:layout_height="fill_parent" 
                android:id="@+id/list_view"  
    />

然后列表视图的内容:screen3.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  >
<TextView android:text="@+id/TextView01" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/label"
        android:textColor="#FF0000"
        android:textSize="30px"></TextView>
  </LinearLayout>

为table_layout.xml设置背景,以便它按预期应用于整个列表视图。

如果您发现任何困难,请告诉我。

感谢Venky。

答案 1 :(得分:0)

你的xml文件看起来很好。尝试实施以下提到: -

制作想要列表视图每行的大小的图像。并将其设置为父LinearLayout的背景。设置: - android:layout_width =“fill_parent”android:layout_height =“wrap_content”。

希望这有帮助!! :)