RelativeLayout系列:动态修改内部View的位置

该博客介绍了如何在Android的RelativeLayout中通过编程方式改变子View的位置。通过使用LayoutParams的addRule方法,可以实现`花花`从左侧移动到右侧,并调整其在`btn2`上方的效果。示例代码展示了如何在按钮点击事件中切换`花花`的位置,使其在`btn1`和`btn2`之间切换显示。
摘要由CSDN通过智能技术生成
  • 上效果图
“花花”显示在左侧“花花”显示在右侧
  • RelativeLayout内部没有setAbovesetBelow这样的设置,那么,如果想修改子view的位置,该怎么用代码控制?
    RelativeLayout使用LayoutParams来控制位置的, RelativeLayout.LayoutParams中有个addRule()。效果图代码如下:
public class MainActivity extends AppCompatActivity {
   TextView tv;
   Button btn1, btn2;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       btn1 = findViewById(R.id.btn1);
       btn2 = findViewById(R.id.btn2);
       tv = findViewById(R.id.tv);


   }

   boolean b = false;
   public void change(View view) {
       if (b) {
           btn1.setVisibility(View.GONE);
           RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) tv.getLayoutParams();
           lp.addRule(RelativeLayout.ABOVE, R.id.btn2);
           lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
           tv.setLayoutParams(lp);

           b = false;
       } else {
           btn1.setVisibility(View.VISIBLE);
           RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) tv.getLayoutParams();
           lp.addRule(RelativeLayout.ABOVE, R.id.btn1);
           lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,0);
           tv.setLayoutParams(lp);

           b = true;
       }
   }
}

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="花花"
        android:layout_above="@id/btn2"
        android:layout_alignParentRight="true"
      />

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:visibility="gone"
        android:text="可隐藏" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text="固定显示" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="change"
        android:text="test"
        />

</RelativeLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangjin1120

可靠的文章费时费力,希望支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值