萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 安卓開發 >> android動態布局之動態加入TextView和ListView的方法

android動態布局之動態加入TextView和ListView的方法

   本文實例講述了android動態布局之動態加入TextView和ListView的方法。分享給大家供大家參考。具體實現方法如下:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 package org.guoshi; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.guoshi.adapter.ImageAndTextAdapter; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.LinearLayout; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView; public class Main extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.friend_info_view); final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.groups); final ListView lv = new ListView(this); List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("title", "jayqean"); map.put("imgsrc", R.drawable.icon); data.add(map); ListAdapter adapter = new ImageAndTextAdapter(Main.this, data, R.layout.chats_view_item, new String[] { "title", "imgsrc" }, new int[] { R.id.chats_view_name, R.id.chats_view_item_image }); lv.setAdapter(adapter); final TextView tv1 = new TextView(this); tv1.setText("常用聯系人"); tv1.setId(1); final RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); lp1.addRule(RelativeLayout.BELOW, R.id.groups); tv1.setLayoutParams(lp1); tv1.setBackgroundColor(R.color.group_view_background); tv1.setOnClickListener(new OnClickListener() { boolean flag = false; @Override public void onClick(View v) { // TODO Auto-generated method stub Log.d("tag", tv1.getText().toString()); if(!flag){ linearLayout.addView(lv, linearLayout.indexOfChild(tv1) + 1); // lp1.addRule(RelativeLayout.BELOW, 1); // linearLayout.addView(lv, lp1); flag = true; } else{ linearLayout.removeView(lv); flag = false; } } }); linearLayout.addView(tv1, lp1); // 線性布局 通過參數index控制加入的控件的位置 // ------------------------ // 加入分割線 final TextView line = new TextView(this); line.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 1)); line.setBackgroundColor(Color.WHITE); linearLayout.addView(line, 1); // ------------------------ final ListView lv2 = new ListView(this); List<Map<String, Object>> data2 = new ArrayList<Map<String, Object>>(); Map<String, Object> map2 = new HashMap<String, Object>(); map2.put("title", "xiaobei"); map2.put("imgsrc", R.drawable.icon); data2.add(map2); ListAdapter adapter2 = new ImageAndTextAdapter(Main.this, data2, R.layout.chats_view_item, new String[] { "title", "imgsrc" }, new int[] { R.id.chats_view_name, R.id.chats_view_item_image }); lv2.setAdapter(adapter2); final TextView tv2 = new TextView(this); tv2.setText("離線好友"); tv2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); tv2.setBackgroundColor(R.color.group_view_background); tv2.setOnClickListener(new OnClickListener() { boolean flag = false; @Override public void onClick(View v) { // TODO Auto-generated method stub Log.d("tag", tv2.getText().toString()); if(!flag){ linearLayout.addView(lv2, linearLayout.indexOfChild(tv2) + 1); flag = true; } else{ linearLayout.removeView(lv2); flag = false; } } }); linearLayout.addView(tv2, 2); } }

  控制布局,可以通過RelativeLayout.LayoutParams類

  ?

1 2 3 4 5 6 7 final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.groups); final TextView tv1 = new TextView(this); tv1.setText("常用聯系人"); RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); lp1.addRule(RelativeLayout.BELOW, R.id.groups); tv1.setLayoutParams(lp1); linearLayout.addView(tv1, lp1);

  也可采用linearLayout.addView(tv1, 0); // 線性布局 通過參數index控制加入的控件的位置

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
copyright © 萬盛學電腦網 all rights reserved