android网易跟帖盖楼布局
16lz
2021-01-26
看网易新闻,盖楼这东西挺有意思,这样的布局还要用xml文件来写吗?楼层少还可以,如果几十楼。。。。写着玩的,代码没整理,仅供新手参考。如有高见或不解,可评论留下。
项目在附件。
activity_main.xml
- <RelativeLayoutxmlns: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
- publicclassMainActivityextendsActivity{
- privatefinalintONE=1;
- privatefinalintTWO=2;
- privatefinalintTHREE=3;
- privatefinalintLAYOUTID=4;
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- 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)));
- }
- privateRelativeLayoutlastLayout(intx,RelativeLayoutlayout){
- if(x==5){
- returnlayout;
- }else{
- x++;
- layout=getLayout(x,layout);
- returnlastLayout(x,layout);
- }
- }
- privateRelativeLayoutgetLayout(intid,RelativeLayoutprelayout){
- prelayout.setId(LAYOUTID*id);
- RelativeLayout.LayoutParamsparams=newLayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
- RelativeLayoutlayout=newRelativeLayout(this);
- layout.setLayoutParams(params);
- layout.setBackgroundResource(R.drawable.layout_bg);
- layout.setPadding(3,3,3,3);
- layout.addView(prelayout);
- TextViewtextView2=newTextView(this);
- textView2.setId(TWO*id);
- textView2.setText(id+"楼昵称");
- textView2.setTextSize(10);
- textView2.setTextColor(Color.BLUE);
- RelativeLayout.LayoutParamstextParams1=newLayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
- textParams1.addRule(RelativeLayout.BELOW,LAYOUTID*id);
- textParams1.topMargin=15;
- textParams1.leftMargin=5;
- layout.addView(textView2,textParams1);
- TextViewtextView=newTextView(this);
- textView.setText(id+"楼回复内容...");
- textView.setId(ONE*id);
- RelativeLayout.LayoutParamstextParams2=newLayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
- textParams2.addRule(RelativeLayout.BELOW,TWO*id);
- textParams2.leftMargin=5;
- layout.addView(textView,textParams2);
- TextViewtextView3=newTextView(this);
- textView3.setId(THREE*id);
- textView3.setText(String.valueOf(id));
- textView3.setTextSize(10);
- RelativeLayout.LayoutParamstextParams3=newLayoutParams(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);
- returnlayout;
- }
- privateRelativeLayoutgetFirst(intid,RelativeLayoutlayout){
- RelativeLayout.LayoutParamsparams=newLayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
- layout.setLayoutParams(params);
- layout.setBackgroundResource(R.drawable.layout_bg);
- layout.setPadding(3,3,3,3);
- RelativeLayout.LayoutParamstextParams1=newLayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
- textParams1.leftMargin=5;
- TextViewtextView2=newTextView(this);
- textView2.setId(TWO*id);
- textView2.setText(id+"楼昵称");
- textView2.setTextSize(10);
- textView2.setTextColor(Color.BLUE);
- layout.addView(textView2,textParams1);
- TextViewtextView=newTextView(this);
- textView.setText(id+"楼回复内容...");
- textView.setId(ONE*id);
- RelativeLayout.LayoutParamstextParams2=newLayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
- textParams2.addRule(RelativeLayout.BELOW,TWO*id);
- textParams2.leftMargin=5;
- layout.addView(textView,textParams2);
- TextViewtextView3=newTextView(this);
- textView3.setId(THREE*id);
- textView3.setText(String.valueOf(id));
- textView3.setTextSize(10);
- RelativeLayout.LayoutParamstextParams3=newLayoutParams(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);
- returnlayout;
- }
- }
更多相关文章
- AndroidTestCase来进行android 单元测试简介
- android UiAutomator自定义快速调试类
- Android(安卓)Studio的使用第一篇优化篇
- 个性化TextView内容
- Android(安卓)ContentProvider query参数
- Android(安卓)SDK Tutorials系列 - Hello Views - Auto Complete
- Android(安卓)Dialog中的EditText无法弹出输入法
- 【Android(安卓)Developers Training】 94. 创建一个空内容提供
- 内容实现android共享Action内容实现