ListView with your own layout

As stated in last post, A simple ListView using android.R.layout.simple_list_item_1 layout, we can copy and modify the built-in layout to implement our own layout in ListView.



ListView with your own layout



Copy the file android-sdk-linux_x86/platforms/android-7/data/res/layout/layout/simple_list_item_1.xml, rename mylistlayout.xml, and paste into /res/layout/ folder of your project. Modify it:

/res/layout/mylistlayout.xml

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@android:id/text1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textStyle="bold|italic"

android:gravity="center_vertical|right"

android:paddingRight="30dip"

android:minHeight="?android:attr/listPreferredItemHeight"

android:drawableLeft="@drawable/icon"

/>





Modify the onCreate() method to use R.layout.mylistlayout in our ListView

    @Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

myList = (ListView)findViewById(R.id.list);



ArrayAdapter<String> adapter

= new ArrayAdapter<String>(this,

R.layout.mylistlayout,

listContent);

myList.setAdapter(adapter);



}





next:

- Load ListView contents from XML resources



0 Response to "ListView with your own layout"

Posting Komentar