Android代码布局

来源:互联网 发布:国资委党员网络培训 编辑:程序博客网 时间:2024/06/01 12:12

    我站在风口浪尖紧握住日月旋转,made,最近被洗脑了....诸位见谅,

    从哥入坑的那一天,一直用的XML布局,听别人说代码布局怎么怎么滴,矮油喂,好像很吊的样子,不知道是不是真的,今天,哥带头装个比给大家演示一下 代码布局经典案案例....

   依旧是以实战为例,如下图这个吊布局,

 


        黑色部分是固定的布局,红色箭头部分就是不确定性布局,有人说这可以用listview,我就不用! 强行来一波动态代码布局!!!!(其实是listview不方便后续操作)

      首先我们看红色箭头,既然要在布局里展示,我们给他一个父控件,然后在父控件中addview的形式添加 子布局,如下代码所示:id为 ll_little;

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@color/bg_gray"    android:orientation="vertical">    <include layout="@layout/top_layout_vip"></include>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="@color/text_white_color"        android:gravity="center_vertical"        android:padding="10dp">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="品类"            />        <LinearLayout            android:id="@+id/ll_choose"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:orientation="horizontal"            android:padding="5dp">            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="请选择"                />            <ImageView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginLeft="5dp"                android:background="@drawable/yingshou_you2" />        </LinearLayout>    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="2dp"        android:background="@color/text_white_color"        android:gravity="center_vertical"        android:padding="10dp">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="品类"            />        <LinearLayout            android:id="@+id/ll_choose2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:orientation="horizontal"            android:padding="5dp">            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="请选择"                />            <ImageView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginLeft="5dp"                android:background="@drawable/yingshou_you2" />        </LinearLayout>    </LinearLayout>    <LinearLayout        android:id="@+id/ll_little"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical">    </LinearLayout></LinearLayout>

以 此LinearLayout 为父布局,好的,装比完毕 开始进入正题:

首先我们先开始箭头所示 一行布局最外层的套子:




   LinearLayout ll_out = new LinearLayout(context);  //里面的布局                            ll_out.setBackgroundResource(R.color.text_white_color);                            LinearLayout.LayoutParams lp_out = new LinearLayout.LayoutParams(                                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams                                    .WRAP_CONTENT);                            ll_out.setPadding(10, 10, 10, 10);                            lp_out.setMargins(0, 2, 0, 0);

可以看到,我们给最外层的套子 设置了个 Layoutparams  ,可以把它看成linearout的 约束规范, lp_out 设置了个setmargins 表示 距离上面 像素点 2, ll_out 色值 setPadding对应着 xml中的 Padding=“10dp”,当然这里表示的是像素,如果你想精准到dp,可以用代码转 dp转px , 这么一设置,表示 我现在有个 Linearout的 布局,并且pading为10 ,同时,我距离上一个布局距离为2,


下面我们继续 ,里面有个 Textview ,和一个 textview与一个imagineview 的结合体,我们可以继续这样做,

<pre name="code" class="java">  TextView tv_out = new TextView(context);                            tv_out.setText("适用年龄段");                            LinearLayout ll_in = new LinearLayout(<span style="font-family: Arial, Helvetica, sans-serif;">context</span><span style="font-family: Arial, Helvetica, sans-serif;">);  //里面的布局</span>                            ll_in.setPadding(5, 5, 5, 5);                            TextView textView_in = new TextView(context<span style="font-family: Arial, Helvetica, sans-serif;">);</span>
                            ImageView imageView = new ImageView(context);                            imageView.setBackgroundResource(R.drawable.yingshou_you2);                            textView_in.setText("请选择");                            LinearLayout.LayoutParams lp_iv = new LinearLayout.LayoutParams(                                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);                            lp_iv.setMargins(5, 0, 0, 0);                            ll_in.addView(textView_in);                            ll_in.addView(imageView, lp_iv);                            ll_out.addView(tv_out);                            ll_out.addView(ll_in);
                            ll_little.addView(ll_out, lp_out);

代码如上,原理一样,多的只是对控件做的一些限制,向设置背景啊 

imageView.setBackgroundResource(R.drawable.yingshou_you2);
设置约束啊:
lp_iv.setMargins(5, 0, 0, 0); 表示距离左边的控件像素为5,
然后就是 父控件add子view 了,这里要说一下,在add子view的时候,可以把子view 的约束一起加进去,这样,子view的约束才有效果 如:
ll_little.addView(ll_out, lp_out);  
 然后这就是一个item的代码,怎么实现多个item,很简单,for循环:
献上所有代码:
  for (int i = 0; i < list_props.size(); i++) {                            LinearLayout ll_out = new LinearLayout(context);  //里面的布局                            ll_out.setBackgroundResource(R.color.text_white_color);                            LinearLayout.LayoutParams lp_out = new LinearLayout.LayoutParams(                                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams                                    .WRAP_CONTENT);                            ll_out.setPadding(10, 10, 10, 10);                            lp_out.setMargins(0, 2, 0, 0);                            TextView tv_out = new TextView(context);                            tv_out.setText(list_props.get(i).getProp_name());                            LinearLayout ll_in = new LinearLayout(context);  //里面的布局                            ll_in.setPadding(5, 5, 5, 5);                            TextView textView_in = new TextView(context);                            ImageView imageView = new ImageView(context);                            imageView.setBackgroundResource(R.drawable.yingshou_you2);                            textView_in.setText("请选择");                            LinearLayout.LayoutParams lp_iv = new LinearLayout.LayoutParams(                                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);                            lp_iv.setMargins(5, 0, 0, 0);                            ll_in.addView(textView_in);                            ll_in.addView(imageView, lp_iv);                            ll_out.addView(tv_out);                            ll_out.addView(ll_in);                            ll_little.addView(ll_out, lp_out);                        }                    }


OK,装逼 完毕,另外附上 qq群:48310355
有兴趣的朋友可以一起讨论研究 Android技术,



0 0