安卓在主布局文件中显示子布局

本文详细介绍如何在Android应用的主布局中动态加载子布局。通过创建XML布局文件,使用LinearLayout作为显示控件,并在MainActivity中通过按钮点击事件触发子布局的加载。此方法适用于需要动态改变UI的应用场景。

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

需求:

在主布局中显示子布局

step1:

创建一个xml布局文件,设置好您想设置的布局内容

step2:

在mainactivity中声明一个控件,之后会显示在那边。我选择了linerlayout。

step3:

声明view并显示

下面是java文件

public class MainActivity extends AppCompatActivity {
    Button button;
    private LinearLayout frameSwitch;//显示控件
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=findViewById(R.id.button);
        frameSwitch = findViewById(R.id.frameSwitch);//声明
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View pictureView = getLayoutInflater().inflate(R.layout.verify, null);

                frameSwitch.addView(pictureView, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
                //                frameSwitch.removeAllViews();//这句话是清除自布局,这里不需要
            }
        });
    }
}

下面是xml文件

主布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="match_parent">

        <Button
            android:layout_width="150dp"
            android:layout_height="100dp"
            android:id="@+id/button"
            android:text="按钮"/>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/frameSwitch"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="10"
        android:orientation="horizontal">
    </LinearLayout>

</LinearLayout>

子布局:

<?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:orientation="vertical">

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:id="@+id/text"
        android:textSize="52dp"
        android:text="姓名:"
        />
    <EditText
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:textSize="52dp"
        />
</LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:id="@+id/text2"
            android:textSize="52dp"
            android:text="密码:"
            />
        <EditText
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:id="@+id/editText2"
            android:textSize="52dp"
            />
    </LinearLayout>
    <Button
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:layout_gravity="center_horizontal"
        android:text="按钮"
        />
</LinearLayout>

效果图:(点击按钮显示布局)

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

wL魔法师

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

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

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

打赏作者

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

抵扣说明:

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

余额充值