Android中怎么用纯代码编写布局?

来源:互联网 发布:精仿雨轩qq教程网源码 编辑:程序博客网 时间:2024/05/02 04:20

一、用RelativeLayout进行纯代码布局的理论基础

     1、RelativeLayout,顾名思义,就是以“相对”位置/对齐 为基础的布局方式。

     2、android.widget.RelativeLayout 有个继承自android.view.ViewGroup.LayoutParams 的内嵌类 LayoutParams,使用这个类的实例

         调用RelativeLayout.addView 就可以实现“相对布局”。 android.widget.RelativeLayout.LayoutParams 有一个构造函数:

         RelativeLayout.LayoutParams(int w, int h),参数指定了子 View 的宽度和高度,这一点和其父类是一样的。而实现相对布局的关 

        键在它的 两个 addRule 方法上。anchor 参数指定可以是View 的 id(“相对于谁”)、RelativeLayout.TRUE(启用某种对齐方式) 或者

        是-1(应用于某些不需要 anchor 的 verb);AddRule 方法的verb 参数指定相对的“动作”(以下常量均定义于

        android.widget.RelativeLayout中,为了简便不给出其全名):
  3、ALIGN_BOTTOM、ALIGN_LEFT、 ALIGN_RIGHT、 ALIGN_TOP: 本 View 的 底边/左边/右边/顶边 和 anchor 指定的 View 的 

           底边/左边/右边/顶边 对齐。 
         ALIGN_WITH_PARENT_BOTTOM 、ALIGN_WITH_PARENT_LEFT 、 ALIGN_WITH_PARENT_RIGHT 、 

         ALIGN_WITH_PARENT_TOP : 和上面一组常量类似,只不过不需要再指定 anchor, 其 anchor 自动为 Parent View。
        CENTER_HORIZONTAL、CENTER_IN_PARENT 、CENTER_VERTICAL : 如果 anchor 为 TRUE,在 Parent 中 水平居中/水平 

         和垂直均居中/垂直居中。
        POSITION_ABOVE 、POSITION_BELOW 、 POSITION_TO_LEFT 、POSITION_TO_RIGHT : 本 View 位于 anchor 指定的 View 

        的上边/下边/左边/右边。

二、案例

     1、布局文件如下

         

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF" >

    <com.baidu.mapapi.map.MapView
        android:id="@+id/baidu_map_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true" >
    </com.baidu.mapapi.map.MapView>

    <RelativeLayout
        android:id="@+id/anquan_map_l1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="5dp"
        android:layout_marginTop="10dp" >

        <ImageButton
            android:id="@+id/but_of_lukuang"
            android:layout_width="38.0dip"
            android:layout_height="38.0dip"
            android:background="@drawable/main_map_button_bg"
            android:src="@drawable/maptraffic_icon_off" />

        <ImageButton
            android:id="@+id/btn_of_bobao"
            android:layout_width="38.0dip"
            android:layout_height="38.0dip"
            android:layout_below="@id/but_of_lukuang"
            android:layout_marginTop="5dp"
            android:visibility="gone"
            android:background="@drawable/main_map_button_bg"
            android:src="@drawable/netfriend_bobao_n" />

        <ImageButton
            android:id="@+id/btn_of_layer"
            android:layout_width="38.0dip"
            android:layout_height="38.0dip"
            android:layout_below="@+id/btn_of_bobao"
            android:layout_marginTop="5dp"
            android:background="@drawable/main_map_button_bg"
            android:src="@drawable/main_map_icon_layer" />
    </RelativeLayout>

</RelativeLayout>

2、代码如下

    //得到 

  mapButtonRL = (RelativeLayout) findViewById(R.id.anquan_map_l1);
  RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
    ViewGroup.LayoutParams.WRAP_CONTENT,
    ViewGroup.LayoutParams.WRAP_CONTENT);
  lp1.addRule(RelativeLayout.BELOW, R.id.btn_of_layer);
  showModeButton = new Button(this);
  showModeButton.setText("全部显示");
  showModeButton.setId(SHOW_MODE);
  showModeButton.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {

   }
  });
  mapButtonRL.addView(showModeButton, lp1);
  RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
    ViewGroup.LayoutParams.WRAP_CONTENT,
    ViewGroup.LayoutParams.WRAP_CONTENT);
  lp2.addRule(RelativeLayout.BELOW, SHOW_MODE);
  positionButton = new Button(this);
  positionButton.setText("位置");
  positionButton.setId(POSITION);
  positionButton.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {

   }
  });
  mapButtonRL.addView(positionButton, lp2);