LinearLayout, RelativeLayout,代码(动态)设置布局位置(线性布局,相对布局)

有些时候我们需要动态的设置-某些布局的位置,(也是代码适配)

一:父布局是,线性布局:

xml文件如下
<LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/id_about_data_4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="6dp"
                android:lineSpacingMultiplier="1.1"
                android:text="省钱。"
                android:textColor="@color/color_666666"
                android:textSize="@dimen/text_font_14" />
        </LinearLayout>
代码设置,因为父布局是一个线性布局,所以我们直接new 一个线性布局的参数,然后在给当前需要改变位置的空间设置最新的参数(相当于xml中设置)

*代码如下

LinearLayout.LayoutParams lp_4 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp_4.leftMargin = needLeftMargn;
        lp_4.topMargin = 12;

        id_about_data_4.setLayoutParams(lp_4);

*如此动态设置完成

二:父布局是,相对布局

xml布局如下
<RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/id_tv_top_captrue_top_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="6dp"
                android:lineSpacingMultiplier="1.1"
                android:text="省钱。"
                android:textColor="@color/color_666666"
                android:textSize="@dimen/text_font_14" />
        </RelativeLayout>
动态设置代码
 RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DensityUtils.dp2px(70));//工具类哦
        layoutParams.topMargin = topHeight;

//这个api设置相对布局,位置的                params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

        id_tv_top_captrue_top_1.setLayoutParams(layoutParams);

*如此动态设置完成

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值