android动态布局之动态加入TextView和ListView的方法

android动态布局之动态加入TextView和ListView的方法,第1张

概述本文实例讲述了android动态布局之动态加入TextView和ListView的方法。分享给大家供大家参考。具体实现方法如下:

本文实例讲述了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> 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.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,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,VIEwGroup parent,int resource) {   VIEw v;   if (convertVIEw == null) {    v = mInflater.inflate(resource,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,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程序设计有所帮助。

总结

以上是内存溢出为你收集整理的android动态布局之动态加入TextView和ListView的方法全部内容,希望文章能够帮你解决android动态布局之动态加入TextView和ListView的方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1142326.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存