开发者

How to change appearance of ListView?

I have three main things I want to change with the appearance of a ListVie开发者_Python百科w. 1) get rid of horizontal dividers that print between items 2) change font size of test displayed in listview 3) change the padding around each item displayed

It looks like after reading for a while that 2) and 3) should be controlled by changing these properties for the TextView used by the ListView, but I don't yet understand how to do that. Can someone answer these with a little more detail?


1) Set divider height to 0 --- setDividerHeight(0) and set divider color to transparent --- setDivider(new ColorDrawable(0x00FFFFFF))

2) If you're using a list of text views then you can continue using a simple adapter like ArrayAdapter but you will need to create a custom text view. You can add something like this to res/layout test_text.xml

<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize=<SIZE IN SP>
android:textColor="#FFF" />

3) Add padding to your textview above

<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text"
android:padding="5dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize=<SIZE IN SP>
android:textColor="#FFF" />

Use your new layout with your ArrayAdapter

eg.
ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)

ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this, R.layout.test_text, R.id.text, myData);


All of those can be controlled via styles. The docs on styles explain everything you need to do.

In short, you create a style resource - derived from whatever you want to use as your base - where you override all the relevant items, like the divider, the padding the font size.

Then, you either apply that style to your ListView in the XML file, or you create a theme to it and call setTheme() BEFORE you call setContentView().

EDIT: I should mention the obvious answer that kcoppock pointed out - if this is specific to just one element in your layout, just modify that particular element in your layout file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜