自定义样式列表分隔器 [英] Custom style listdivider

查看:22
本文介绍了自定义样式列表分隔器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将默认的 ListView 列表分隔符设置为在两侧都有空白边距.我已经尝试设置整个 ListView 的边距,但这并没有产生预期的结果.

I'm trying to style the default ListView list divider to have margins of whitespace on both sides. I already tried to set the margins of the entire ListView but that didn't create the desired result.

我有什么:

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

What i'm trying to make (notice the margins on both sides of the dividers):

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

What would be the best way of doing this? Thanks in advance!

推荐答案

使用 和自定义布局线.像这样:

use <include layout="@layout/divider" /> and custom layout for line. like this:

ma​​in.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();       
            }
      });
    }
}

这篇关于自定义样式列表分隔器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆