2014-09-12 64 views
-1

我想设置我的列表视图的背景喜欢在链接的图片中,它应该显示为按钮,但我不能单独设置背景。如何在ListView上设置背景

http://www.delta.edu/images/OIT/Android%20List%20View.png

我试过android:listSelector="@drawable/pic"和这对我来说没有工作。我也试过tools:listitem="@layout/row",但它没有为我工作。

我希望我清楚我的问题。

+0

您必须在适配器的xml的xml中设置背景 – 2014-09-12 14:09:51

+0

您需要为该行设置背景(如果您正在讨论如图中所示的设置按钮/卡片背景)。在这种情况下,只需更改行xml以获得背景。 – Srikanth 2014-09-12 14:10:13

+0

你想为你的所有应用程序设置一个共同的背景,还是只为ListView设置一个特定的背景? – 2014-09-12 14:10:22

回答

0

查看tutorial like this的一些提示。

创建绘制类似的XML文件这样的:

rounded_corners.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" > 

    <solid android:color="#00FF00"/> 

    <corners android:radius="5dp" /> 

    <padding android:left="3dp" android:top="3dp" android:right="3dp" android:bottom="3dp" /> 

    <stroke android:width="3dp" android:color="#00CC00"/> 
</shape> 

listview_selector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:drawable="@drawable/rounded_corner" android:state_enabled="true"/> 

    <item android:drawable="@drawable/rounded_corner" android:state_pressed="true"/> 

    <item android:drawable="@drawable/rounded_corner" android:state_focused="true"/> 

</selector> 

它应用到类似的ListView适配器这个:

public View getView(int position, View convertView, ViewGroup parent) { 
    View v = convertView; 

    PlanetHolder holder = new PlanetHolder(); 

    // First let's verify the convertView is not null 
    if (convertView == null) { 
     // This a new view we inflate the new layout 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = inflater.inflate(R.layout.row_layout, null); 
     // Now we can fill the layout with the right values 
     TextView tv = (TextView) v.findViewById(R.id.name); 

     holder.planetNameView = tv; 

     v.setTag(holder); 

     v.setBackgroundResource(R.drawable.rounded_corner); 
    } 
    else 
     holder = (PlanetHolder) v.getTag(); 

    Planet p = planetList.get(position); 
    holder.planetNameView.setText(p.getName()); 

    return v; 
}