看网易新闻,盖楼这东西挺有意思,这样的布局还要用xml文件来写吗?楼层少还可以,如果几十楼。。。。写着玩的,代码没整理,仅供新手参考。如有高见或不解,可评论留下。

项目在附件。

activity_main.xml

  1. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="#ffffff"
  6. tools:context=".MainActivity">
  7. <RelativeLayout
  8. android:id="@+id/layout1"
  9. android:layout_height="wrap_content"
  10. android:layout_width="fill_parent"
  11. android:layout_margin="10dp"
  12. ></RelativeLayout>
  13. </RelativeLayout>

MainActivity.java

        
  1. publicclassMainActivityextendsActivity{
  2. privatefinalintONE=1;
  3. privatefinalintTWO=2;
  4. privatefinalintTHREE=3;
  5. privatefinalintLAYOUTID=4;
  6. @Override
  7. protectedvoidonCreate(BundlesavedInstanceState){
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main);
  10. setContentView(R.layout.activity_main); RelativeLayout allLayout=(RelativeLayout) findViewById(R.id.layout1); RelativeLayout layoutFirst = new RelativeLayout(this); allLayout.addView(lastLayout(1, getFirst(1, layoutFirst)));
  11. }
  12. privateRelativeLayoutlastLayout(intx,RelativeLayoutlayout){
  13. if(x==5){
  14. returnlayout;
  15. }else{
  16. x++;
  17. layout=getLayout(x,layout);
  18. returnlastLayout(x,layout);
  19. }
  20. }
  21. privateRelativeLayoutgetLayout(intid,RelativeLayoutprelayout){
  22. prelayout.setId(LAYOUTID*id);
  23. RelativeLayout.LayoutParamsparams=newLayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
  24. RelativeLayoutlayout=newRelativeLayout(this);
  25. layout.setLayoutParams(params);
  26. layout.setBackgroundResource(R.drawable.layout_bg);
  27. layout.setPadding(3,3,3,3);
  28. layout.addView(prelayout);
  29. TextViewtextView2=newTextView(this);
  30. textView2.setId(TWO*id);
  31. textView2.setText(id+"楼昵称");
  32. textView2.setTextSize(10);
  33. textView2.setTextColor(Color.BLUE);
  34. RelativeLayout.LayoutParamstextParams1=newLayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
  35. textParams1.addRule(RelativeLayout.BELOW,LAYOUTID*id);
  36. textParams1.topMargin=15;
  37. textParams1.leftMargin=5;
  38. layout.addView(textView2,textParams1);
  39. TextViewtextView=newTextView(this);
  40. textView.setText(id+"楼回复内容...");
  41. textView.setId(ONE*id);
  42. RelativeLayout.LayoutParamstextParams2=newLayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
  43. textParams2.addRule(RelativeLayout.BELOW,TWO*id);
  44. textParams2.leftMargin=5;
  45. layout.addView(textView,textParams2);
  46. TextViewtextView3=newTextView(this);
  47. textView3.setId(THREE*id);
  48. textView3.setText(String.valueOf(id));
  49. textView3.setTextSize(10);
  50. RelativeLayout.LayoutParamstextParams3=newLayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
  51. textParams3.rightMargin=5;
  52. textParams3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
  53. textParams3.addRule(RelativeLayout.ALIGN_BASELINE,TWO*id);
  54. layout.addView(textView3,textParams3);
  55. returnlayout;
  56. }
  57. privateRelativeLayoutgetFirst(intid,RelativeLayoutlayout){
  58. RelativeLayout.LayoutParamsparams=newLayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
  59. layout.setLayoutParams(params);
  60. layout.setBackgroundResource(R.drawable.layout_bg);
  61. layout.setPadding(3,3,3,3);
  62. RelativeLayout.LayoutParamstextParams1=newLayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
  63. textParams1.leftMargin=5;
  64. TextViewtextView2=newTextView(this);
  65. textView2.setId(TWO*id);
  66. textView2.setText(id+"楼昵称");
  67. textView2.setTextSize(10);
  68. textView2.setTextColor(Color.BLUE);
  69. layout.addView(textView2,textParams1);
  70. TextViewtextView=newTextView(this);
  71. textView.setText(id+"楼回复内容...");
  72. textView.setId(ONE*id);
  73. RelativeLayout.LayoutParamstextParams2=newLayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
  74. textParams2.addRule(RelativeLayout.BELOW,TWO*id);
  75. textParams2.leftMargin=5;
  76. layout.addView(textView,textParams2);
  77. TextViewtextView3=newTextView(this);
  78. textView3.setId(THREE*id);
  79. textView3.setText(String.valueOf(id));
  80. textView3.setTextSize(10);
  81. RelativeLayout.LayoutParamstextParams3=newLayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
  82. textParams3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
  83. textParams3.addRule(RelativeLayout.ALIGN_BASELINE,TWO*id);
  84. textParams3.rightMargin=5;
  85. layout.addView(textView3,textParams3);
  86. returnlayout;
  87. }
  88. }

更多相关文章

  1. AndroidTestCase来进行android 单元测试简介
  2. android UiAutomator自定义快速调试类
  3. Android(安卓)Studio的使用第一篇优化篇
  4. 个性化TextView内容
  5. Android(安卓)ContentProvider query参数
  6. Android(安卓)SDK Tutorials系列 - Hello Views - Auto Complete
  7. Android(安卓)Dialog中的EditText无法弹出输入法
  8. 【Android(安卓)Developers Training】 94. 创建一个空内容提供
  9. 内容实现android共享Action内容实现

随机推荐

  1. 《Google!Android2手机应用程序设计入门》
  2. android modbus协议之(三)modbus-TCP/IP
  3. GTS李万鹏:Android游戏开发者的亲身感受
  4. Unity与EasyAR
  5. Android(安卓)开发自己的网络收音机4——
  6. Android基站信息获取以及Sim卡相关信息获
  7. Android在开发中的实用技巧之ExpandableL
  8. Android(安卓)多用户
  9. Android开发:Android的ListView下拉刷新,基
  10. Google解决专利权第一步