android网易跟帖盖楼布局
看网易新闻,盖楼这东西挺有意思,这样的布局还要用xml文件来写吗?楼层少还可以,如果几十楼。。。。写着玩的,代码没整理,仅供新手参考。如有高见或不解,可评论留下。
项目在附件。
activity_main.xml
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#ffffff"
- tools:context=".MainActivity" >
- <RelativeLayout
- android:id="@+id/layout1"
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:layout_margin="10dp"
- ></RelativeLayout>
- </RelativeLayout>
MainActivity.java
- public class MainActivity extends Activity {
- private final int ONE=1;
- private final int TWO=2;
- private final int THREE=3;
- private final int LAYOUTID=4;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setContentView(R.layout.activity_main); RelativeLayout allLayout=(RelativeLayout) findViewById(R.id.layout1); RelativeLayout layoutFirst = new RelativeLayout(this); allLayout.addView(lastLayout(1, getFirst(1, layoutFirst)));
- }
- private RelativeLayout lastLayout(int x,RelativeLayout layout){
- if(x==5){
- return layout;
- }else{
- x++;
- layout=getLayout(x, layout);
- return lastLayout(x,layout);
- }
- }
- private RelativeLayout getLayout(int id,RelativeLayout prelayout){
- prelayout.setId(LAYOUTID*id);
- RelativeLayout.LayoutParams params=new LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
- RelativeLayout layout=new RelativeLayout(this);
- layout.setLayoutParams(params);
- layout.setBackgroundResource(R.drawable.layout_bg);
- layout.setPadding(3, 3, 3, 3);
- layout.addView(prelayout);
- TextView textView2=new TextView(this);
- textView2.setId(TWO*id);
- textView2.setText(id+"楼昵称");
- textView2.setTextSize(10);
- textView2.setTextColor(Color.BLUE);
- RelativeLayout.LayoutParams textParams1=new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout. LayoutParams.WRAP_CONTENT);
- textParams1.addRule(RelativeLayout.BELOW,LAYOUTID*id);
- textParams1.topMargin=15;
- textParams1.leftMargin=5;
- layout.addView(textView2,textParams1);
- TextView textView=new TextView(this);
- textView.setText(id+"楼回复内容...");
- textView.setId(ONE*id);
- RelativeLayout.LayoutParams textParams2=new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout. LayoutParams.WRAP_CONTENT);
- textParams2.addRule(RelativeLayout.BELOW,TWO*id);
- textParams2.leftMargin=5;
- layout.addView(textView,textParams2);
- TextView textView3=new TextView(this);
- textView3.setId(THREE*id);
- textView3.setText(String.valueOf(id));
- textView3.setTextSize(10);
- RelativeLayout.LayoutParams textParams3=new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
- textParams3.rightMargin=5;
- textParams3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
- textParams3.addRule(RelativeLayout.ALIGN_BASELINE,TWO*id);
- layout.addView(textView3,textParams3);
- return layout;
- }
- private RelativeLayout getFirst(int id,RelativeLayout layout){
- RelativeLayout.LayoutParams params=new LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
- layout.setLayoutParams(params);
- layout.setBackgroundResource(R.drawable.layout_bg);
- layout.setPadding(3, 3, 3, 3);
- RelativeLayout.LayoutParams textParams1=new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout. LayoutParams.WRAP_CONTENT);
- textParams1.leftMargin=5;
- TextView textView2=new TextView(this);
- textView2.setId(TWO*id);
- textView2.setText(id+"楼昵称");
- textView2.setTextSize(10);
- textView2.setTextColor(Color.BLUE);
- layout.addView(textView2,textParams1);
- TextView textView=new TextView(this);
- textView.setText(id+"楼回复内容...");
- textView.setId(ONE*id);
- RelativeLayout.LayoutParams textParams2=new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout. LayoutParams.WRAP_CONTENT);
- textParams2.addRule(RelativeLayout.BELOW,TWO*id);
- textParams2.leftMargin=5;
- layout.addView(textView,textParams2);
- TextView textView3=new TextView(this);
- textView3.setId(THREE*id);
- textView3.setText(String.valueOf(id));
- textView3.setTextSize(10);
- RelativeLayout.LayoutParams textParams3=new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
- textParams3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
- textParams3.addRule(RelativeLayout.ALIGN_BASELINE,TWO*id);
- textParams3.rightMargin=5;
- layout.addView(textView3,textParams3);
- return layout;
- }
- }
转载于:https://blog.51cto.com/zhichengandroid/1175235