listview頁面html
- package com.test.querylist;
- import java.util.ArrayList;
- import java.util.HashMap;
- import com.test.R;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.HorizontalScrollView;
- import android.widget.LinearLayout;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- import android.widget.LinearLayout.LayoutParams;
- public class QueryListActivity extends Activity {
- private static String str1 = "str1";
- private static String str2 = "str2";
- private static String str3 = "str3";
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- setContentView(generateContentView());
- }
- private View generateContentView(){
- //主頁面
- LinearLayout layMain = createLayout(LinearLayout.VERTICAL);
- layMain.setPadding(5, 0, 5, 0);
- //查詢部分
- LinearLayout layQuery = createLayout(LinearLayout.HORIZONTAL);
- layQuery.setPadding(0, 0, 0, 0);
- layQuery.setGravity(Gravity.CENTER);
- generateQuery(layQuery);
- //列表部分
- LinearLayout layList = createLayout(LinearLayout.VERTICAL);
- layList.setPadding(0, 0, 0, 0);
- generateList(layList);
- layMain.addView(layQuery);
- layMain.addView(layList);
- return layMain;
- }
- /**
- * 列表部分佈局
- * */
- private void generateList(LinearLayout layList) {
- HorizontalScrollView hsView = new HorizontalScrollView(this);
- hsView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
- LinearLayout lly = createLayout(LinearLayout.VERTICAL);
- // TODO Auto-generated method stub
- ArrayList<HashMap<String, Object>> myLists = new ArrayList<HashMap<String, Object>>();
- for (int i = 0; i < 10; i++) {
- HashMap<String, Object> myList = new HashMap<String, Object>();
- myList.put(str1, "asd");
- myList.put(str2, "姓名(" + (i+1) + ")");
- myList.put(str3, (5 + i) + "");
- myLists.add(myList);
- }
- SimpleAdapter saImageItems = new SimpleAdapter(this, myLists,// 數據來源
- R.layout.testuser,// 每個user xml 至關ListView的一個組件
- new String[] { str1, str2, str3 },
- // 分別對應view 的id
- new int[] { R.id.test_id1, R.id.test_id2, R.id.test_id3 });
- // 獲取listview
- ListView listQuery = new ListView(this);
- listQuery.setAdapter(saImageItems);
- lly.addView(listQuery);
- hsView.addView(lly);
- layList.addView(hsView);
- }
- /**
- * 查詢部分佈局
- * */
- private void generateQuery(LinearLayout layQuery) {
- // TODO Auto-generated method stub
- EditText txtQuery = new EditText(this);
- txtQuery.setLayoutParams(new LayoutParams(400, LayoutParams.WRAP_CONTENT));
- txtQuery.setMinHeight(60);
- txtQuery.setMaxHeight(120);
- Button btnQuery = new Button(this);
- btnQuery.setText(R.string.querylist_query);
- btnQuery.setOnClickListener(new Button.OnClickListener(){
- public void onClick(View v) {
- // TODO 設置點擊事件
- }
- });
- layQuery.addView(txtQuery);
- layQuery.addView(btnQuery);
- }
- private LinearLayout createLayout(int iOrientation) {
- LinearLayout lay = new LinearLayout(this);
- lay.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
- lay.setOrientation(iOrientation);
- return lay;
- }
- }
xmljava
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_height="wrap_content" android:layout_width="fill_parent"
- android:paddingBottom="5dip" android:paddingTop="5dip"
- android:paddingLeft="5dip" android:paddingRight="5dip"
- android:orientation="vertical">
- <LinearLayout android:layout_height="wrap_content"
- android:paddingLeft="6dip" android:paddingRight="6dip"
- android:orientation="horizontal" android:layout_width="fill_parent"
- android:gravity="center_vertical">
- <TextView android:id="@+id/test_id1" android:gravity="center"
- android:layout_height="wrap_content" android:layout_width="300"
- android:layout_weight="1" android:layout_margin = "5dip">
- </TextView>
- <TextView android:id="@+id/test_id2" android:gravity="center"
- android:layout_height="wrap_content" android:layout_width="300"
- android:layout_weight="1" android:layout_margin = "5dip">
- </TextView>
- <TextView android:id="@+id/test_id3" android:gravity="center"
- android:layout_height="wrap_content" android:layout_width="300"
- android:layout_weight="1" android:layout_margin = "5dip">
- </TextView>
- </LinearLayout>
- </LinearLayout>
轉自:http://www.cnblogs.com/QiuLee/archive/2011/04/20/2022920.htmlandroid