通过代码实现RelativeLayout的布局

一:布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/rl_product_parent" 
    android:background="@drawable/login_bg">
    <RelativeLayout 
        android:id="@+id/rl_product_main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="51dp"
        >
       
    <ImageView 
        android:id="@+id/iv_jiuyingbao"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:src="@drawable/jiuyingbao"/>
    <ImageView 
        android:id="@+id/iv_zhinengcun"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/iv_jiuyingbao"
        android:layout_alignTop="@id/iv_jiuyingbao"
        android:src="@drawable/zhinengcun"/>
    <ImageView 
        android:id="@+id/iv_diydai"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@id/iv_zhinengcun"
        android:layout_toRightOf="@id/iv_jiuyingbao"
        android:layout_alignBottom="@id/iv_jiuyingbao"
        android:src="@drawable/diydai"/>
    <ImageView 
        android:id="@+id/iv_wodezhanghu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/iv_jiuyingbao"
        android:layout_alignLeft="@id/iv_jiuyingbao"
        android:layout_alignRight="@id/iv_diydai"
        android:layout_marginTop="5dp"
        android:src="@drawable/wodezhanghu"/>
    <ImageView 
        android:id="@+id/iv_dangmianfu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/iv_wodezhanghu"
        android:layout_alignLeft="@id/iv_wodezhanghu"
        android:layout_marginTop="5dp"
        android:src="@drawable/dangminafu"/>
     <ImageView 
         android:id="@+id/iv_zhuanzhang"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignTop="@id/iv_dangmianfu"
         android:layout_alignRight="@+id/iv_wodezhanghu"
         android:src="@drawable/zhuanzhang"/>
      </RelativeLayout>
</RelativeLayout>

二:java代码

		rl_product_main = (RelativeLayout) main.findViewById(R.id.rl_product_main);
		RelativeLayout.LayoutParams rparams = new RelativeLayout.LayoutParams(rl_product_main.getLayoutParams());
		//说明当前相对布局距离父控件的顶部100像素,注意是像素
		rparams.topMargin = 100;
		//宽高是包裹内容
		rparams.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
		rparams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
		/**
		 * 一个参数的方法:addRule (int verb)
		 * 给当前相对布局添加一个布局规则,即一些属性等,但是这个布局规则不能涉及到其他兄弟姐妹;或者这个布局规则是一个布尔类型的,设置了即为true,不设置即为false
		 * 两个参数的方法:addRule (int verb, int anchor)
		 * 给当前相对布局添加一个布局规则,这个布局规则可以涉及到其它兄弟姐妹元素,此时第二个参数即为所涉及到的兄弟姐妹的id;或者这个布局规则是一个布尔类型的,此时
		 * 第二个参数即为RelativeLayout.TRUE代表为true或者0代表为false
		 */
		rparams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
		rl_product_main.setLayoutParams(rparams);




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值