Android App中使用ListFragment的实例教程

2019-12-10 18:02:20丽君

说明:

(01) 在 onCreateView()中,调用list_fragment_self作为该ListFragment的布局文件。
(02) 在 onCreate()中,通过setListAdapter() 设置R.layout.two_textview为ListView每一行的布局文件。


list_fragment_self.xml的内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://www.easck.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >
 
 <!-- ListFragment对应的android:id值固定为"@id/android:list" -->
 <ListView
  android:id="@id/android:list"
  android:layout_width="match_parent"
  android:layout_height="match_parent" 
  android:drawSelectorOnTop="false"
  />
 
</LinearLayout>

two_textview.xml的内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://www.easck.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >
 
 <TextView android:id="@+id/text1"
  android:textSize="12sp"
  android:textStyle="bold"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"/>

 <TextView android:id="@+id/text2"
  android:textSize="24sp"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"/>
  
</LinearLayout>

 

效果图:

Android,ListFragment