android中动态布局.doc_第1页
android中动态布局.doc_第2页
android中动态布局.doc_第3页
android中动态布局.doc_第4页
android中动态布局.doc_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

android中动态布局(动态加入TextView和ListView)分类:Android2011-04-21 16:201068人阅读评论(1)收藏举报javaview plaincopy1. packageorg.guoshi;2. importjava.util.ArrayList;3. importjava.util.HashMap;4. importjava.util.List;5. importjava.util.Map;6. importorg.guoshi.adapter.ImageAndTextAdapter;7. importandroid.app.Activity;8. importandroid.graphics.Color;9. importandroid.os.Bundle;10. importandroid.util.Log;11. importandroid.view.View;12. importandroid.view.View.OnClickListener;13. importandroid.view.ViewGroup.LayoutParams;14. importandroid.widget.LinearLayout;15. importandroid.widget.ListAdapter;16. importandroid.widget.ListView;17. importandroid.widget.RelativeLayout;18. importandroid.widget.TextView;19. publicclassMainextendsActivity20. /*Calledwhentheactivityisfirstcreated.*/21. Override22. publicvoidonCreate(BundlesavedInstanceState)23. super.onCreate(savedInstanceState);24. setContentView(R.layout.friend_info_view);25. finalLinearLayoutlinearLayout=(LinearLayout)findViewById(R.id.groups);26. 27. finalListViewlv=newListView(this);28. ListMapdata=newArrayListMap();29. Mapmap=newHashMap();30. map.put(title,jayqean);31. map.put(imgsrc,R.drawable.icon);32. data.add(map);33. ListAdapteradapter=newImageAndTextAdapter(Main.this,data,R.layout.chats_view_item,newStringtitle,imgsrc,newint34. R.id.chats_view_name,35. R.id.chats_view_item_image);36. lv.setAdapter(adapter);37. finalTextViewtv1=newTextView(this);38. tv1.setText(常用联系人);39. tv1.setId(1);40. finalRelativeLayout.LayoutParamslp1=newRelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);41. lp1.addRule(RelativeLayout.BELOW,R.id.groups);42. tv1.setLayoutParams(lp1);43. tv1.setBackgroundColor(R.color.group_view_background);44. tv1.setOnClickListener(newOnClickListener()45. booleanflag=false;46. Override47. publicvoidonClick(Viewv)48. /TODOAuto-generatedmethodstub49. Log.d(tag,tv1.getText().toString();50. if(!flag)51. linearLayout.addView(lv,linearLayout.indexOfChild(tv1)+1);52. /lp1.addRule(RelativeLayout.BELOW,1);53. /linearLayout.addView(lv,lp1);54. flag=true;55. else56. linearLayout.removeView(lv);57. flag=false;58. 59. 60. 61. );62. linearLayout.addView(tv1,lp1);/线性布局通过参数index控制加入的控件的位置63. /-64. /加入分割线65. finalTextViewline=newTextView(this);66. line.setLayoutParams(newLayoutParams(LayoutParams.FILL_PARENT,1);67. line.setBackgroundColor(Color.WHITE);68. linearLayout.addView(line,1);69. /-70. finalListViewlv2=newListView(this);71. ListMapdata2=newArrayListMap();72. Mapmap2=newHashMap();73. map2.put(title,xiaobei);74. map2.put(imgsrc,R.drawable.icon);75. data2.add(map2);76. ListAdapteradapter2=newImageAndTextAdapter(Main.this,data2,R.layout.chats_view_item,newStringtitle,imgsrc,newint77. R.id.chats_view_name,78. R.id.chats_view_item_image);79. lv2.setAdapter(adapter2);80. 81. finalTextViewtv2=newTextView(this);82. tv2.setText(离线好友);83. tv2.setLayoutParams(newLayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);84. tv2.setBackgroundColor(R.color.group_view_background);85. tv2.setOnClickListener(newOnClickListener()86. booleanflag=false;87. Override88. publicvoidonClick(Viewv)89. /TODOAuto-generatedmethodstub90. Log.d(tag,tv2.getText().toString();91. if(!flag)92. linearLayout.addView(lv2,linearLayout.indexOfChild(tv2)+1);93. flag=true;94. else95. linearLayout.removeView(lv2);96. flag=false;97. 98. 99. 100. );101. linearLayout.addView(tv2,2);102. 103. 104. 105. 控制布局,可以通过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控制加入的控件的位置javaview plaincopy1. packageorg.guoshi.adapter;2. importjava.util.List;3. importjava.util.Map;4. importandroid.content.Context;5. importandroid.graphics.Bitmap;6. importandroid.graphics.BitmapFactory;7. importandroid.util.Log;8. importandroid.view.LayoutInflater;9. importandroid.view.View;10. importandroid.view.View.OnClickListener;11. importandroid.view.ViewGroup;12. importandroid.widget.Checkable;13. importandroid.widget.ImageView;14. importandroid.widget.SimpleAdapter;15. importandroid.widget.TextView;16. publicclassImageAndTextAdapterextendsSimpleAdapter17. privateContextmcontext;18. privateintmTo;19. privateStringmFrom;20. privateViewBindermViewBinder;21. 22. privateList?extendsMapmData;23. privateintmResource;24. privateLayoutInflatermInflater;25. publicImageAndTextAdapter(Contextcontext,26. List?extendsMapdata,intresource,Stringfrom,27. intto)28. super(context,data,resource,from,to);29. mcontext=context;30. mData=data;31. mResource=resource;32. mFrom=from;33. mTo=to;34. mInflater=(LayoutInflater)context35. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);36. /mInflater=LayoutInflater.from(mcontext);37. 38. /*39. *seeandroid.widget.Adapter#getView(int,View,ViewGroup)40. */41. publicViewgetView(intposition,ViewconvertView,ViewGroupparent)42. returncreateViewFromResource(position,convertView,parent,mResource);43. 44. privateViewcreateViewFromResource(intposition,ViewconvertView,45. ViewGroupparent,intresource)46. Viewv;47. if(convertView=null)48. v=mInflater.inflate(resource,parent,false);49. finalintto=mTo;50. finalintcount=to.length;51. finalViewholder=newViewcount;52. for(inti=0;icount;i+)53. holderi=v.findViewById(toi);54. 55. v.setTag(holder);56. else57. v=convertView;58. 59. 60. bindView(position,v);61. 62. /finalintindex=position;63. /v.setOnClickListener(newOnClickListener()64. /65. /publicvoidonClick(Viewv)66. /TODOAuto-generatedmethodstub67. /Log.d(item,index+);68. /69. /);70. returnv;71. 72. 73. privatevoidbindView(intposition,Viewview)74. 75. finalMapdataSet=mData.get(position);76. if(dataSet=null)77. return;78. 79. finalViewBinderbinder=mViewBinder;80. finalViewholder=(View)view.getTag();81. finalStringfrom=mFrom;82. finalintto=mTo;83. finalintcount=to.length;84. for(inti=0;icount;i+)85. finalViewv=holderi;86. if(v!=null)87. finalObjectdata=dataSet.get(fromi);88. Stringtext=data=null?:data.toString();89. if(text=null)90. text=;91. 92. booleanbound=false;93. if(binder!=null)94. bound=binder.setViewValue(v,data,text);95. 96. if(!bound)97. if(vinstanceofCheckable)98. if(datainstanceofBoolean)99. (Checkable)v).setChecked(Boolean)data);100. else101. thrownewIllegalStateException(v.getClass()102. .getName()103. +shouldbeboundtoaBoolean,nota104. +data.getClass();105. 106. elseif(vinstanceofTextView)107. setViewText(TextView)v,text);108. elseif(vinstanceofImageView)109. if(datainstanceofInteger)110. setViewImage(ImageView)v,(Integer)data);111. else112. setViewImage(ImageView)v,text);113. 114. else115. thrownewIllegalStateException(116. v.getClass().getName()117. +isnota118. +viewthatcanbeboundsbythisSimpleAdapter);119. 120. 121. 122. 123. 124. /*125. *CalledbybindView()tosettheimageforanImageViewbutonlyifthere126. *isnoexistingViewBinderoriftheexistingViewBindercannothandle127. *bindingtoanImageView.128. *129. *Thismethodiscalledinsteadoflink#setViewImage(ImageView,String)130. *ifthesupplieddataisanintorInteger.131. *132. *paramv133. *ImageViewtoreceiveanimage134. *paramvalue135. *thevalueretrievedfromthedataset136. *137. *see#setViewImage(ImageView,String)138. */139. publicvoidsetViewImage(ImageViewv,intvalue)140. v.setImageResource(value);141. 142. /*143. *CalledbybindView()tosetthei

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论