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

本文详细介绍了Android中RelativeLayout布局的特点及使用方法,包括如何通过addRule方法设置视图间的相对位置,并通过一个具体的案例展示了如何动态地添加按钮并指定其相对位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  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、布局文件如下
         

http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF" >
    
        android:id="@+id/baidu_map_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true" >
    
    
        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" >
        
            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" />
        
            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" />
        
            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" />
    

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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值