现在的位置: 首页 > 综合 > 正文

【代码生成Android布局】例.用户信息界面的生成

2018年02月17日 ⁄ 综合 ⁄ 共 5079字 ⁄ 字号 评论关闭

   1.先看下最终要达到的效果图:


     2.新建Activity布局文件activity_common_info.xml

<? 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= "#eee"
    android:orientation= "vertical" >
    <ScrollView
        android:layout_width= "fill_parent"
        android:layout_height= "fill_parent"
        android:scrollbars= "@null" >
        <LinearLayout
            android:id= "@+id/ll_content"
            android:layout_width= "fill_parent"
            android:layout_height= "wrap_content"
            android:orientation= "vertical"
            android:paddingLeft= "20dip"
            android:paddingRight= "20dip"
            android:paddingTop= "20dip" >
        </LinearLayout >
    </ScrollView >
</ LinearLayout>
     3.创建实体类RowItem.java 包含两个成员变量key和value,如key是"姓名",value是"张三"。

public class RowItem {
             private String key;
             private String value;
             public RowItem(String key, String value) {
                         super();
                         this. key = key;
                         this. value = value;
            }
             public String getKey() {
                         return key;
            }
             public String getValue() {
                         return value;
            }
}
     4.创建行布局,本文为了美观用到三种行样式,这里仅提供一种样式布局如下,其他两个类似。
   
<? 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:orientation= "vertical" >
    <LinearLayout
        android:layout_width= "fill_parent"
        android:layout_height= "wrap_content"
        android:background= "@drawable/about_mid_bg"
        android:gravity= "center_vertical"
        android:orientation= "horizontal"
        android:padding= "8dip" >
        <TextView
            android:id= "@+id/tv_key"
            android:layout_width= "60dip"
            android:layout_height= "30dip"
            android:layout_marginLeft= "8dip"
            android:gravity= "center_vertical"
            android:textColor= "#222222"
            android:textSize= "14sp" />
        <TextView
            android:id= "@+id/tv_value"
            android:layout_width= "fill_parent"
            android:layout_height= "30dip"
            android:gravity= "center_vertical"
            android:paddingLeft= "8dip"
            android:textColor= "#333333"
            android:textSize= "14sp" />
    </LinearLayout >
</ LinearLayout>
  
5.创建自动生成行的工具类CreateViewUtil.java

  public class CreateViewUtil {
             public static void createContentView(Context context,LinearLayout contentLayout,List<RowItem> rowItemList) {
                        View childView;
                        TextView keyTv;
                        TextView valueTv;
                        LayoutInflater layoutInflater;
                        layoutInflater = LayoutInflater. from(context);
                         for ( int i = 0; i < rowItemList.size(); i++) {
                                     if (i == 0) {
                                                childView = layoutInflater.inflate(R.layout.row_top_item null);
                                    } else if (i == (rowItemList.size() - 1)) {
                                                childView = layoutInflater.inflate(R.layout.row_bottom_item null );
                                    } else {
                                                childView = layoutInflater.inflate(R.layout.row_mid_item null);
                                    }
                                    keyTv = (TextView) childView.findViewById(R.id. tv_key);
                                    valueTv = (TextView) childView.findViewById(R.id. tv_value);
                                    keyTv.setText(rowItemList.get(i).getKey());
                                    valueTv.setText(rowItemList.get(i).getValue());
                                    contentLayout.addView(childView);
                        }
            }
}
     6.最后创建Activity-----AutoLayoutActivity.class

public class AutoLayoutActivity extends Activity {
             private LinearLayout contentLl;
             private List<RowItem> mList;
             @Override
             protected void onCreate(Bundle savedInstanceState) {
                         super.onCreate(savedInstanceState);
                        setContentView(R.layout. activity_common_info);
                         contentLl = (LinearLayout) findViewById(R.id. ll_content);
                         mList = new ArrayList<RowItem>();
                         mList.add( new RowItem( "姓名""张三"));
                         mList.add( new RowItem( "性别""男"));
                         mList.add( new RowItem( "年龄""28"));
                         mList.add( new RowItem( "学历""博士"));
                        CreateViewUtil. createContentView( thiscontentLlmList);
            }
}
 释疑:
         大家或许会问,就"姓名","性别"几行而已直接在布局文件中写死不是更快,为什么弄得这么麻烦。
这样做的目的是当项目中有大量这种类似的页面时,这种方式方便快捷,多个类似的Activity可以共用一个布局文件就可以,不需要写很长的布局文件,更不需要一个一个的找到控件把值设置进去。总之一句话,能少写一行代码就少写一行。
 



抱歉!评论已关闭.