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

项目在附件。

android网易跟帖盖楼布局

activity_main.xml

 

  1. <RelativeLayout xmlns: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.  
  8.     <RelativeLayout  
  9.         android:id="@+id/layout1" 
  10.         android:layout_height="wrap_content" 
  11.         android:layout_width="fill_parent" 
  12.         android:layout_margin="10dp" 
  13.         ></RelativeLayout> 
  14.  
  15. </RelativeLayout> 

 

MainActivity.java

 


  1. public class MainActivity extends Activity { 
  2.  
  3.     private final int ONE=1
  4.     private final int TWO=2
  5.     private final int THREE=3
  6.     private final int LAYOUTID=4
  7.     @Override 
  8.     protected void onCreate(Bundle savedInstanceState) { 
  9.         super.onCreate(savedInstanceState); 
  10.         setContentView(R.layout.activity_main); 
  11.         setContentView(R.layout.activity_main); RelativeLayout allLayout=(RelativeLayout) findViewById(R.id.layout1); RelativeLayout layoutFirst = new RelativeLayout(this); allLayout.addView(lastLayout(1, getFirst(1, layoutFirst)));
  12.     } 
  13.      
  14.        private RelativeLayout lastLayout(int x,RelativeLayout layout){ 
  15.            if(x==5){ 
  16.                return layout; 
  17.            }else
  18.                x++; 
  19.                layout=getLayout(x, layout); 
  20.                return lastLayout(x,layout); 
  21.            } 
  22.        } 
  23.         
  24.     private RelativeLayout getLayout(int id,RelativeLayout prelayout){ 
  25.         prelayout.setId(LAYOUTID*id); 
  26.         RelativeLayout.LayoutParams params=new LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
  27.         RelativeLayout layout=new RelativeLayout(this); 
  28.         layout.setLayoutParams(params); 
  29.         layout.setBackgroundResource(R.drawable.layout_bg); 
  30.         layout.setPadding(3333); 
  31.         layout.addView(prelayout); 
  32.         TextView textView2=new TextView(this); 
  33.         textView2.setId(TWO*id); 
  34.         textView2.setText(id+"楼昵称"); 
  35.         textView2.setTextSize(10); 
  36.         textView2.setTextColor(Color.BLUE); 
  37.         RelativeLayout.LayoutParams textParams1=new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout. LayoutParams.WRAP_CONTENT); 
  38.         textParams1.addRule(RelativeLayout.BELOW,LAYOUTID*id); 
  39.         textParams1.topMargin=15
  40.         textParams1.leftMargin=5
  41.         layout.addView(textView2,textParams1); 
  42.         TextView textView=new TextView(this); 
  43.         textView.setText(id+"楼回复内容..."); 
  44.         textView.setId(ONE*id); 
  45.         RelativeLayout.LayoutParams textParams2=new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout. LayoutParams.WRAP_CONTENT); 
  46.         textParams2.addRule(RelativeLayout.BELOW,TWO*id); 
  47.         textParams2.leftMargin=5
  48.         layout.addView(textView,textParams2); 
  49.         TextView textView3=new TextView(this); 
  50.         textView3.setId(THREE*id); 
  51.         textView3.setText(String.valueOf(id)); 
  52.         textView3.setTextSize(10); 
  53.         RelativeLayout.LayoutParams textParams3=new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
  54.         textParams3.rightMargin=5
  55.         textParams3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
  56.         textParams3.addRule(RelativeLayout.ALIGN_BASELINE,TWO*id); 
  57.         layout.addView(textView3,textParams3); 
  58.         return layout; 
  59.     } 
  60.     private RelativeLayout getFirst(int id,RelativeLayout layout){ 
  61.         RelativeLayout.LayoutParams params=new LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
  62.         layout.setLayoutParams(params); 
  63.         layout.setBackgroundResource(R.drawable.layout_bg); 
  64.         layout.setPadding(3333); 
  65.         RelativeLayout.LayoutParams textParams1=new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout. LayoutParams.WRAP_CONTENT); 
  66.         textParams1.leftMargin=5
  67.         TextView textView2=new TextView(this); 
  68.         textView2.setId(TWO*id); 
  69.         textView2.setText(id+"楼昵称"); 
  70.         textView2.setTextSize(10); 
  71.         textView2.setTextColor(Color.BLUE); 
  72.         layout.addView(textView2,textParams1); 
  73.         TextView textView=new TextView(this); 
  74.         textView.setText(id+"楼回复内容..."); 
  75.         textView.setId(ONE*id); 
  76.         RelativeLayout.LayoutParams textParams2=new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout. LayoutParams.WRAP_CONTENT); 
  77.         textParams2.addRule(RelativeLayout.BELOW,TWO*id); 
  78.         textParams2.leftMargin=5
  79.         layout.addView(textView,textParams2); 
  80.         TextView textView3=new TextView(this); 
  81.         textView3.setId(THREE*id); 
  82.         textView3.setText(String.valueOf(id)); 
  83.         textView3.setTextSize(10); 
  84.         RelativeLayout.LayoutParams textParams3=new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
  85.         textParams3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
  86.         textParams3.addRule(RelativeLayout.ALIGN_BASELINE,TWO*id); 
  87.         textParams3.rightMargin=5
  88.         layout.addView(textView3,textParams3); 
  89.         return layout; 
  90.     } 
  91.