亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 系統 > Android > 正文

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

2020-04-11 11:32:08
字體:
來源:轉載
供稿:網友

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

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類

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控制加入的控件的位置

package org.guoshi.adapter; import java.util.List; import java.util.Map; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Checkable; import android.widget.ImageView; import android.widget.SimpleAdapter; import android.widget.TextView; public class ImageAndTextAdapter extends SimpleAdapter {  private Context mcontext;  private int[] mTo;  private String[] mFrom;  private ViewBinder mViewBinder;  private List<? extends Map<String, ?>> mData;  private int mResource;  private LayoutInflater mInflater;  public ImageAndTextAdapter(Context context,    List<? extends Map<String, ?>> data, int resource, String[] from,    int[] to) {   super(context, data, resource, from, to);   mcontext = context;   mData = data;   mResource = resource;   mFrom = from;   mTo = to;   mInflater = (LayoutInflater) context     .getSystemService(Context.LAYOUT_INFLATER_SERVICE); //  mInflater = LayoutInflater.from(mcontext);  }  /**   * @see android.widget.Adapter#getView(int, View, ViewGroup)   */  public View getView(int position, View convertView, ViewGroup parent) {   return createViewFromResource(position, convertView, parent, mResource);  }  private View createViewFromResource(int position, View convertView,   ViewGroup parent, int resource) {   View v;   if (convertView == null) {    v = mInflater.inflate(resource, parent, false);    final int[] to = mTo;    final int count = to.length;    final View[] holder = new View[count];    for (int i = 0; i < count; i++) {     holder[i] = v.findViewById(to[i]);    }    v.setTag(holder);   } else {    v = convertView;   }   bindView(position, v); //  final int index = position; //  v.setOnClickListener(new OnClickListener() { //   //   public void onClick(View v) { //    // TODO Auto-generated method stub //    Log.d("item", index + ""); //   } //  });   return v;  }  private void bindView(int position, View view) {   final Map<String, ?> dataSet = mData.get(position);   if (dataSet == null) {    return;   }   final ViewBinder binder = mViewBinder;   final View[] holder = (View[]) view.getTag();   final String[] from = mFrom;   final int[] to = mTo;   final int count = to.length;   for (int i = 0; i < count; i++) {    final View v = holder[i];    if (v != null) {     final Object data = dataSet.get(from[i]);     String text = data == null ? "" : data.toString();     if (text == null) {      text = "";     }     boolean bound = false;     if (binder != null) {      bound = binder.setViewValue(v, data, text);     }     if (!bound) {      if (v instanceof Checkable) {       if (data instanceof Boolean) {        ((Checkable) v).setChecked((Boolean) data);       } else {        throw new IllegalStateException(v.getClass()          .getName()          + " should be bound to a Boolean, not a "          + data.getClass());       }      } else if (v instanceof TextView) {       setViewText((TextView) v, text);      } else if (v instanceof ImageView) {       if (data instanceof Integer) {        setViewImage((ImageView) v, (Integer) data);       } else {        setViewImage((ImageView) v, text);       }      } else {       throw new IllegalStateException(         v.getClass().getName()           + " is not a "           + " view that can be bounds by this SimpleAdapter");      }     }    }   }  }  /**   * Called by bindView() to set the image for an ImageView but only if there   * is no existing ViewBinder or if the existing ViewBinder cannot handle   * binding to an ImageView.   *   * This method is called instead of {@link #setViewImage(ImageView, String)}   * if the supplied data is an int or Integer.   *   * @param v   *   ImageView to receive an image   * @param value   *   the value retrieved from the data set   *   * @see #setViewImage(ImageView, String)   */  public void setViewImage(ImageView v, int value) {   v.setImageResource(value);  }  /**   * Called by bindView() to set the image for an ImageView but only if there   * is no existing ViewBinder or if the existing ViewBinder cannot handle   * binding to an ImageView.   *   * By default, the value will be treated as an image resource. If the value   * cannot be used as an image resource, the value is used as an image Uri.   *   * This method is called instead of {@link #setViewImage(ImageView, int)} if   * the supplied data is not an int or Integer.   *   * @param v   *   ImageView to receive an image   * @param value   *   the value retrieved from the data set   *   * @see #setViewImage(ImageView, int)   */  public void setViewImage(ImageView v, String value) {   Bitmap bitMap = BitmapFactory.decodeFile(value);   v.setImageBitmap(bitMap);  } }

下面是friend_info_view.xml

<?xml version="1.0" encoding="UTF-8"?> <!-- 好友信息列表.xml --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent"  android:layout_height="fill_parent" android:background="#ffffff">  <RelativeLayout android:layout_width="wrap_content"   android:layout_height="wrap_content">   <ImageView android:id="@+id/selfImage"    android:adjustViewBounds="true" android:layout_width="@dimen/self_image_width"    android:layout_height="@dimen/self_image_height"    android:layout_marginLeft="5.0dip" android:layout_marginBottom="10.0dip"    android:layout_marginTop="3.0dip" android:src="@drawable/default_image" />   <ImageView android:id="@+id/currentStatus"    android:layout_width="wrap_content" android:layout_height="wrap_content"    android:src="@drawable/status_available" android:layout_marginLeft="8.0dip"    android:layout_marginTop="20.0dip" android:layout_toRightOf="@id/selfImage" />   <TextView android:id="@+id/setStatus" android:layout_width="wrap_content"    android:layout_height="wrap_content" android:layout_marginTop="20.0dip"    android:layout_marginLeft="8.0dip" android:text="Tap here to set your status"    android:layout_toRightOf="@+id/currentStatus" />  </RelativeLayout>  <EditText android:id="@+id/searchFriend"   android:adjustViewBounds="true" android:layout_height="50dip"   android:layout_width="fill_parent" android:text="Search..." />  <!-- 好友組 點擊textview后出現組里的詳細好友列表 -->  <LinearLayout android:id="@+id/groups" android:layout_width="fill_parent"   android:layout_height="wrap_content" android:orientation="vertical" >   </LinearLayout> </LinearLayout>

chats_view_item.xml

<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical" android:layout_width="fill_parent"  android:layout_height="fill_parent" android:background="@color/white">  <RelativeLayout android:id="@+id/chats_view_item"   android:layout_width="wrap_content" android:layout_height="wrap_content">   <ImageView android:id="@+id/chats_view_item_image"    android:layout_width="@dimen/friend_image_width"    android:layout_height="@dimen/friend_image_height"    android:paddingLeft="5.0dip" android:paddingTop="2.0dip"    android:src="@drawable/default_image" />   <TextView android:id="@+id/chats_view_name" android:textSize="14.0sp"    android:paddingLeft="10.0dip" android:textStyle="bold"    android:ellipsize="marquee" android:layout_width="wrap_content"    android:layout_height="wrap_content" android:text="username"    android:singleLine="true" android:paddingTop="2.0dip"    android:layout_toRightOf="@+id/chats_view_item_image" />   <ImageView android:id="@+id/friend_status_icon"    android:layout_width="wrap_content" android:layout_height="wrap_content"    android:paddingLeft="10.0dip" android:paddingTop="1.0dip"    android:layout_below="@+id/chats_view_name" android:layout_toRightOf="@+id/chats_view_item_image"    android:src="@drawable/jabber_available" />   <TextView android:id="@+id/chats_view_status"    android:textColor="@android:color/secondary_text_light"    android:ellipsize="marquee" android:layout_width="fill_parent"    android:layout_height="wrap_content" android:text="available"    android:singleLine="true" android:paddingLeft="2.0dip"    android:layout_toRightOf="@+id/friend_status_icon"    android:layout_below="@+id/chats_view_name" />  </RelativeLayout> </LinearLayout>

效果圖如下:

希望本文所述對大家的Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
亚洲а∨天堂久久精品喷水| 一区二区三区美女xx视频| 亚洲精品www久久久久久广东| 久久久精品视频在线观看| 中文字幕亚洲欧美日韩在线不卡| 91免费人成网站在线观看18| 久久久久久这里只有精品| 国产精品三级在线| 91视频免费网站| 成人h视频在线观看播放| 欧美日韩国产一区中文午夜| 亚洲丝袜一区在线| 日本中文字幕久久看| 5566日本婷婷色中文字幕97| 久久精品91久久香蕉加勒比| 日产精品99久久久久久| 日本成人在线视频网址| 亚洲精品有码在线| 日韩福利在线播放| 狠狠躁夜夜躁人人爽天天天天97| 欧美高跟鞋交xxxxxhd| 日本19禁啪啪免费观看www| 91老司机精品视频| 国内自拍欧美激情| 亚洲人成免费电影| 日韩av影院在线观看| 国产成人精品视频在线| www.欧美精品| 亚洲国产精品99| 91丝袜美腿美女视频网站| 日本韩国欧美精品大片卡二| 亚洲一区二区免费在线| 亚洲第一精品久久忘忧草社区| 在线观看免费高清视频97| 日韩人体视频一二区| 成人精品aaaa网站| 日韩视频亚洲视频| 视频直播国产精品| 亚洲精品一区二三区不卡| 欧美在线欧美在线| 亚洲国产精彩中文乱码av| 亚洲小视频在线观看| 日韩欧美亚洲国产一区| 久久国产精品99国产精| 精品成人69xx.xyz| 久久久这里只有精品视频| 欧美日韩国产精品一区二区不卡中文| 欧美自拍视频在线| 自拍偷拍亚洲精品| 日韩在线激情视频| 中文字幕在线看视频国产欧美| 国产在线视频2019最新视频| 精品一区二区电影| 中文字幕日韩欧美| 久久久久久久国产精品视频| 欧美精品做受xxx性少妇| 欧美一区深夜视频| 欧美中文字幕在线| 欧美日韩激情网| 欧美性猛交xxxx富婆| 欧美二区乱c黑人| 青青久久aⅴ北条麻妃| 国产精品久久久久久久久影视| 欧美性xxxxxxx| 亚洲电影天堂av| 欧美国产第二页| 成人有码在线播放| 91精品成人久久| 国产大片精品免费永久看nba| 日本不卡视频在线播放| 亚洲va久久久噜噜噜久久天堂| 国产精品手机播放| 国产欧美精品一区二区| 国产精品视频网| 国产欧美日韩专区发布| 在线观看国产精品淫| 欧美激情va永久在线播放| 日韩中文字幕国产| 欧美日韩国产一区中文午夜| 国产欧美精品久久久| 亚洲偷熟乱区亚洲香蕉av| 2019最新中文字幕| 在线观看精品国产视频| 亚洲免费电影在线观看| 成人免费网站在线| 欧美激情亚洲视频| 成人深夜直播免费观看| 国产va免费精品高清在线| 国产精品久久一区主播| 热久久美女精品天天吊色| 国产精品av免费在线观看| 亚洲男女自偷自拍图片另类| 亚洲日本中文字幕| 在线不卡国产精品| 亚洲自拍偷拍色片视频| 97国产一区二区精品久久呦| 国a精品视频大全| 精品毛片网大全| 色综合久久精品亚洲国产| 92福利视频午夜1000合集在线观看| 国产色视频一区| 色偷偷噜噜噜亚洲男人的天堂| 欧亚精品中文字幕| 欧美性视频精品| 午夜剧场成人观在线视频免费观看| 国产婷婷97碰碰久久人人蜜臀| 国内精品久久久久久久| 亚洲美女激情视频| 久久久亚洲天堂| 精品久久在线播放| 538国产精品一区二区在线| 亚洲欧美激情另类校园| 国产91免费观看| 美女黄色丝袜一区| 欧美精品亚州精品| 欧美成人一区二区三区电影| 国产成人一区二区三区小说| 亚洲网站在线播放| 久久精品色欧美aⅴ一区二区| 国产99久久精品一区二区| 精品国产拍在线观看| 亚洲a在线观看| 亚洲免费电影在线观看| 亚洲国产美女久久久久| 亚洲日本欧美日韩高观看| 成人高h视频在线| 欧美怡红院视频一区二区三区| 国产精品96久久久久久又黄又硬| 国产亚洲精品久久久久久牛牛| 亚洲电影天堂av| 久久久久久美女| 国产精品久久在线观看| 欧美黑人xxx| 久久99久久久久久久噜噜| 91高清在线免费观看| 97久久精品在线| 国产日本欧美一区| 亚洲精品中文字幕女同| 91精品国产免费久久久久久| 91精品视频专区| 国产精品久久色| 国产精品成熟老女人| zzijzzij亚洲日本成熟少妇| 欧美激情网友自拍| 国产精品18久久久久久麻辣| 福利精品视频在线| 久久亚洲精品一区| 欧美激情精品久久久久久| 国产成人自拍视频在线观看| 亚洲自拍偷拍色图| 91精品国产99久久久久久| 国产一区二区日韩| 国产区亚洲区欧美区| 久久国产精品99国产精| 热久久免费国产视频| 国内精品久久久久久中文字幕| 久久人人97超碰精品888| 国产精品jizz在线观看麻豆| 91人成网站www| 欧美性xxxx极品hd满灌| 色综合伊人色综合网站| 亚洲va久久久噜噜噜久久天堂| 麻豆一区二区在线观看| 亚洲精品国产成人|