I have shared a lot about the android layout before. Thanks to the author of these articles. Let's take a look back:
Introduction to linearlayout and absolutelayout in Android
Introduction to relativelayout and framelayout of Android Layout
Introduction to tablelayout in Android Layout
Tablelayout for Android Layout
Okay, let's talk about the usage of listview in Android. The original Article is as follows:
Listview is a commonly used control. Each item in the listview can be a string or a combination control. First, let's talk about the implementation of listview:
1. Prepare the data to be displayed in listview;
2. Use a one-dimensional or multi-dimensional dynamic array to save data;
2. Build an adapter. In simple terms, the adapter is an item array, and the dynamic array generates as many items as possible;
3. Add the adapter to the listview and display it.
Next, let's take a look at the listview implemented by the code in this article:
Next, we will start the XML code of the UI:
The main. XML Code is as follows:
<?xml version="1.0" encoding="utf-8"?><LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/MyListView"> </ListView></LinearLayout>
The code for my_listitem.xml is as follows. my_listitem.xml is used to design the items of listview:
<?xml version="1.0" encoding="utf-8"?><LinearLayout android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_height="wrap_content" android:id="@+id/MyListItem" android:paddingBottom="3dip" android:paddingLeft="10dip"> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/ItemTitle" android:textSize="30dip"> </TextView> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/ItemText"> </TextView></LinearLayout>
Here are some of the attributes used:
1. paddingbottom = "3dip", layout leaves 3 pixels of blank area at the bottom
2. paddingleft = "10dip", layout leaves a blank area of 10 pixels to the left
3. textsize = "30dip", textview font is as large as 30 pixels.
The source code of Java is as follows:
Public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // bind the listview in XML as the item container listview list = (listview) findviewbyid (R. id. mylistview); // generates a dynamic array and returns the arraylist