自定义样式listdivider

时间:2012-03-08 15:58:52

标签: android android-layout android-listview

我正在尝试将默认的ListView列表分隔符设置为在两边都有空白边距。我已经尝试设置整个ListView的边距,但是没有创建所需的结果。

我有什么:

What i have

我想做什么(注意分隔线两侧的边距):

what i need to have

这样做的最佳方法是什么?提前谢谢!

3 个答案:

答案 0 :(得分:10)

使用<include layout="@layout/divider" />和自定义布局。像这样:

main.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="fill_parent"
    android:orientation="vertical" >
<ListView android:id="@+id/listview"
android:layout_width="wrap_content"
android:divider="@layout/divider"
android:dividerHeight="3sp"
 android:layout_height="wrap_content" />
</LinearLayout>

<强> divider.xml

   <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient 
        android:startColor="#00000000"
        android:centerColor="#FFFFFF"
        android:endColor="#00000000"
        />
    <corners 
        android:radius="4dp"
        />
</shape>

<强> AndroidlistviewdividerActivity.class

public class AndroidlistviewdividerActivity extends Activity {
    /** Called when the activity is first created. */
    private ListView lv;
    private String listview_array[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX",
                                    "SEVEN", "EIGHT", "NINE", "TEN" };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        lv = (ListView) findViewById(R.id.listview);
        lv.setAdapter(new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, listview_array));
        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(new OnItemClickListener()
        {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
             AlertDialog.Builder adb = new AlertDialog.Builder(
                        AndroidlistviewdividerActivity.this);
                adb.setTitle("ListView OnClick");
                adb.setMessage("Selected Item is = "
                + lv.getItemAtPosition(arg2));
                adb.setPositiveButton("Ok", null);
                adb.show();       
        }
      });
    }
}

答案 1 :(得分:6)

我最终能够使用this帖子的接受答案解决我的问题(没有渐变部分)

答案 2 :(得分:0)

我建议您创建自己的ListAdapter并将其设置为ListView。然后,在getView方法中,您可以以任何方式设置单个列表项的样式。