ListActivity 使用方法详解

 原文:android中 ListActivity讲解
  ListActivity的默认布局由一个位于屏幕中心的全屏列表构成。如果你不想使用默认的布局,可以在onCreate()方法中通过setContentView()方法设定你自己的布局。如果指定你自己定制的布局,你的布局中必须包含一个id为"@id/android:list"的ListView。 若你还指定了一个id为"@id/android:empty"的view,当ListView中没有数据要显示时,这个view就会被显示,同时 ListView会被隐藏。下面代码中有注释,这样更能让我们的明白代码中的含义。
1. xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:paddingLeft="8dp"
android:paddingRight="8dp">

<!-- 除了ListView和id为@id/android:empty的view之外,我们还可以任意添加view -->
<TextView 
android:id="@+id/android:title" 
android:layout_width="match_parent"
android:layout_height="wrap_content" 
android:text="The following is a list:" />

<!-- id为@id/android:list的ListView为客户化的list布局,如果没有,则系统会调用默认的布局 -->
<ListView 
android:id="@id/android:list" 
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:background="#00FF00"
android:layout_weight="1" 
android:drawSelectorOnTop="false" />

<!-- 当ListView中没有数据时,id为@id/android:empty的view就会显示出来 -->
<TextView 
android:id="@id/android:empty" 
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:textColor="#FF0000"
android:text="No data" 
android:gravity="center_vertical|center_horizontal" />
</LinearLayout>
2. java 代码:
package EOE.android;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class ListActivityDemo extends ListActivity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		List<String> items = fillList();
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, items);
		setListAdapter(adapter);
	}
	private List<String> fillList() {
	List<String> items = new ArrayList<String>();
	items.add("星期一");
	items.add("星期二");
	items.add("星期三");
	items.add("星期四");
	items.add("星期五");
	items.add("星期六");
	items.add("星期日");
	//items.clear();
	return items;
	}
}
效果图:
  


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值