如何为ListView设置分割线

给ListView设置分割线,只需设置如下两个属性:

android:divider="#000" //设置分割线显示颜色

android:dividerHeight="1px" //此处非0,否则无效

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="#0f0"
        android:dividerHeight="1dp" />

</RelativeLayout>
代码如下:

package com.itfom.listviewdivider;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

	private ListView listview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		listview = (ListView) findViewById(R.id.listview);
		//绑定适配器
		ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[]{"夏守成好帅啊","夏守成好帅啊","夏守成好帅啊","夏守成好帅啊","夏守成好帅啊","夏守成好帅啊","夏守成好帅啊","夏守成好帅啊","夏守成好帅啊","夏守成好帅啊","夏守成好帅啊","夏守成好帅啊","夏守成好帅啊"});
		listview.setAdapter(adapter);
	}
}

运行结果如下所示:

如何为ListView设置分割线_第1张图片

你可能感兴趣的:(android,ListView,分割线)