2011-09-21 108 views
0

我想为我的List视图设置背景。我已从可绘制的图像,并将其设置为布局文件的背景,然后它给出的结果如下屏幕截图screen with background无法将背景设置为ListView

当我只是删除背景获取我需要的东西,但没有背景。 和屏幕截图是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(); 

    } 

而最后,我需要的是我想与适当的列表视图背景图像显示了一个又一个列表项。在此先感谢 }

+0

你在哪里设置背景? –

+0

@radha是否所有ListView的背景都应该相同? – Venky

+0

我已经在xml文件中做了android:background =“@ drawable/screen6” – radha

回答

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。

+0

谢谢venky。我获得了所需的屏幕,但是对布局做了一些更改。我可以在聊天室告诉你 – radha

0

所有看起来都很好的xml文件。尝试执行下面提到的: -

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

希望这有助于! :)