欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-05-27 10:06:09
本文实例讲述了android动态布局之动态加入textview和listview的方法。分享给大家供大家参考。具体实现方法如下: package org.guos...

本文实例讲述了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动态布局之动态加入TextView和ListView的方法

希望本文所述对大家的android程序设计有所帮助。