Android 代码动态修改RelativeLayout布局

Android 代码动态修改RelativeLayout布局

前言

有时我们会遇到在xml布局文件中设置好界面后,但是又需要从代码中进行动态布局修改。
之前从网上寻找了好多资料,看到的都是新建一个布局文件
但是我的需求又是从当前布局文件的基础上进行修改。
下面用一个示例大家介绍一个比较简单的办法。

演示

该例子主要是使用RelativeLayout布局,放置两个TextView,点击切换按钮进行切换,永远有一个TextView保持屏幕中间。
例子.gif

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_dark"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/text_small"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginEnd="6dp"
            android:text="主页面"
            android:textColor="@color/deeppink"
            android:textSize="22sp" />
        <TextView
            android:id="@+id/text_big"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginStart="6dp"
            android:layout_toEndOf="@id/text_small"
            android:text="子页面"
            android:textColor="@color/white"
            android:textSize="12sp" />
    </RelativeLayout>
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="切换" />
</LinearLayout>

MainActivity.java


public class MainActivity extends AppCompatActivity {
    //    TextView
    private TextView text_small;
    private TextView text_big;
    //    布局参数
    private RelativeLayout.LayoutParams params_small;
    private RelativeLayout.LayoutParams params_big;
    //    切换按钮
    private Button btn;
    //    切换标识
    private boolean check = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text_small = findViewById(R.id.text_small);
        text_big = findViewById(R.id.text_big);
        btn = findViewById(R.id.btn);
//        获取TextView的布局参数
        params_small = (RelativeLayout.LayoutParams) text_small.getLayoutParams();
        params_big = (RelativeLayout.LayoutParams) text_big.getLayoutParams();
//          按钮切换
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (check) {
//                    切换子页面
                    smallModeSwitch();
                    check = false;
                } else {
//                    切换主页面
                    bigModeSwitch();
                    check = true;
                }
            }
        });
    }

    /**
     * 修改布局
     *
     * @date: 2021/1/28 16:07
     * @author: SiYuan Jiao
     */
    private void bigModeSwitch() {
//        设置字体及颜色
        text_big.setTextSize(22);
        text_big.setTextColor(Color.parseColor("#ff1493"));
        text_small.setTextSize(12);
        text_small.setTextColor(Color.WHITE);
//        删除主页面居中效果
        params_small.removeRule(RelativeLayout.CENTER_HORIZONTAL);
//        添加主页面对于子页面的相对位置
        params_small.addRule(RelativeLayout.START_OF, R.id.text_big);
//        设置主页面应用此效果
        text_small.setLayoutParams(params_small);
//        添加子页面居中效果
        params_big.addRule(RelativeLayout.CENTER_HORIZONTAL);
//        删除原有相对主页面的位置
        params_big.removeRule(RelativeLayout.END_OF);
//        子页面应用此效果
        text_big.setLayoutParams(params_big);
    }

    private void smallModeSwitch() {
        text_small.setTextSize(22);
        text_small.setTextColor(Color.parseColor("#ff1493"));
        text_big.setTextSize(12);
        text_big.setTextColor(Color.WHITE);


        params_big.removeRule(RelativeLayout.CENTER_HORIZONTAL);
        params_big.addRule(RelativeLayout.END_OF, R.id.text_small);
        text_big.setLayoutParams(params_big);
        params_small.addRule(RelativeLayout.CENTER_HORIZONTAL);
        params_small.removeRule(RelativeLayout.START_OF);
        text_small.setLayoutParams(params_small);
    }

}

结束

其中主要是通过getLayoutParams()方法获取当前布局信息,使用removeRule()删除不要的属性,使用addRule()添加需要的属性值,最后使用setLayoutParams进行应用配置。
通过以上的例子,应该能解决动态代码修改RelativeLayout布局的问题。

感谢

如果对你有用,请点个爱心给个赞吧~~

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android Studio中,动态布局是指在运行时根据特定条件或用户输入来动态地更改应用程序的布局。这种布局方式允许应用程序根据不同的屏幕尺寸、设备方向或其他因素来自适应地调整界面。 要实现动态布局,可以通过以下步骤进行操作: 1. 创建布局文件:首先,在res/layout文件夹中创建一个XML布局文件。这个文件将作为应用程序的初始布局。 2. 使用布局容器:在布局文件中使用适当的布局容器(如LinearLayout、RelativeLayout等)来容纳其他视图元素。 3. 设置视图属性:根据需要,在布局容器中添加视图元素(如按钮、文本框等),并为它们设置相应的属性,如宽度、高度、位置等。 4. 使用代码修改布局:在Java代码中,可以使用布局管理器来动态地更改布局。通过获取对布局容器的引用,可以添加、删除或修改其中的视图元素。 例如,可以使用以下代码将一个新的按钮添加到LinearLayout布局中: ```java LinearLayout layout = findViewById(R.id.myLinearLayout); // 获取对LinearLayout的引用 Button button = new Button(this); // 创建一个新的按钮 button.setText("动态按钮"); // 设置按钮文本 layout.addView(button); // 将按钮添加到LinearLayout中 ``` 通过这种方式,可以根据应用程序的需要动态地更改布局,以适应不同的条件或用户输入。 请注意,动态布局的实现还涉及到处理屏幕旋转、设备尺寸变化等情况时的布局更新和适配。在处理这些情况时,可以使用适配器模式、响应式布局等技术来简化开发过程。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

jiaosiyuan785

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值