Android studio 约束布局嵌套 - > 点击按钮 - > 隐藏布局

今天先要解决的问题是怎么样点击按钮来隐藏掉页面的部分布局。

用的是 ConstraintLayout 约束布局,其实他的嵌套子布局就是ConstraintLayout里面再加ConstraintLayout........

至于隐藏其实就是Visibility的可见性,因为我想要的就是点击按钮隐藏这一部分的布局,之后还要点回来,所以我用的是

ConstraintLayout off = findViewById(R.id.off);
off.setVisibility(View.INVISIBLE)

【1】布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <TextView
        android:layout_width="129dp"
        android:layout_height="60dp"
        android:lineSpacingExtra="27dp"
        android:text="demo"
        android:textColor="#F87461"
        android:textSize="40sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.076" />


    <TextView
        android:layout_width="89dp"
        android:layout_height="39dp"
        android:lineSpacingExtra="27dp"
        android:text="设定时间"
        android:textColor="#ff333333"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.159"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.242" />

    <Button
        android:id="@+id/sendbutton2"
        android:layout_width="81dp"
        android:layout_height="34dp"
        android:background="@drawable/button1"
        android:lineSpacingExtra="17dp"
        android:onClick="onclick2"
        android:text="close"
        android:textColor="#FFFFFF"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.894"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.225" />


    <View
        android:layout_width="292dp"
        android:layout_height="1dp"
        android:layout_marginStart="32dp"
        android:background="@android:color/darker_gray"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.47"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.323"
        android:layout_marginLeft="32dp" />


    <android.support.constraint.ConstraintLayout 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:id="@+id/off"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="56dp">


        <TextView
            android:layout_width="136dp"
            android:layout_height="52dp"
            android:lineSpacingExtra="27dp"
            android:text="开始时间"
            android:textColor="#999999"
            android:textSize="25sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_bias="0.181"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.444" />


        <TextView
            android:layout_width="111dp"
            android:layout_height="46dp"
            android:lineSpacingExtra="27dp"
            android:text="结束时间"
            android:textColor="#999999"
            android:textSize="25sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_bias="0.164"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.65" />

        <ImageView
            android:layout_width="157dp"
            android:layout_height="165dp"
            android:background="@drawable/hhh"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_bias="0.85"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.547" />

        <Button
            android:id="@+id/sendbutton1"
            android:layout_width="295dp"
            android:layout_height="37dp"
            android:background="@drawable/press"
            android:lineSpacingExtra="17dp"
            android:onClick="onclick1"
            android:text="确定"
            android:textColor="#FFFFFF"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_bias="0.505"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.822" />


    </android.support.constraint.ConstraintLayout>


</android.support.constraint.ConstraintLayout>

【2】activity

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;


public class MainActivity extends AppCompatActivity  {

    @Override
    public void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onclick2(View view)
    {
        ConstraintLayout off=findViewById(R.id.off);
        off.setVisibility(View.INVISIBLE);
    }

    public void onclick1(View view)
    {
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("已成功修改时间");//设置对话框的标题
        builder.setNegativeButton("返回", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        AlertDialog b=builder.create();
        b.show();
    }

}

【3】效果图

点击按钮后下面的布局隐藏了

### 回答1: 当你在Android Studio中创建新项目时,默认情况下会使用约束布局(ConstraintLayout)进行界面布局。但你也可以选择使用其他布局方式,例如线性布局(LinearLayout)或相对布局(RelativeLayout)。 以下是使用约束布局进行界面布局的示例: 1. 打开Android Studio并创建新项目。 2. 在res/layout目录中创建一个新的XML布局文件。 3. 将以下代码复制并粘贴到XML文件中: ``` <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textview_hello" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> ``` 4. 运行应用程序并查看界面布局。你会看到一个包含“Hello World!”文本的文本视图。 在这个示例中,我们创建了一个ConstraintLayout布局,并在其中添加了一个TextView。使用app:layout_constraintBottom_toBottomOf,app:layout_constraintEnd_toEndOf,app:layout_constraintStart_toStartOf和app:layout_constraintTop_toTopOf属性将TextView固定在父布局的中心。 你可以尝试添加更多的视图,然后使用约束属性将它们与其他视图和父布局对齐。ConstraintLayout使得布局变得灵活,可扩展,可以在不同屏幕大小和设备上呈现出更好的效果。 ### 回答2: 在Android Studio中,线性布局是一种常用的布局容器,它允许将元素按照水平或垂直方向排列。而线性布局也可以嵌套在其他线性布局中,形成复杂的布局结构。 下面以一个实例来说明线性布局嵌套使用。 假设我们需要创建一个界面,其中包含一个上方导航栏和一个下方显示内容的区域。导航栏中有两个按钮,分别代表“主页”和“设置”。 首先,在布局文件中定义一个垂直方向的根线性布局: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 头部导航栏 --> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:text="主页" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:text="设置" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <!-- 内容区域 --> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 此处添加内容视图 --> </LinearLayout> </LinearLayout> ``` 上述代码中,根线性布局设置了垂直方向,并占满全屏。导航栏部分是一个水平方向的线性布局,包含了两个按钮。内容区域是另一个垂直方向的线性布局,用于显示具体的内容。 我们可以在内容区域的LinearLayout中添加自定义的视图,例如一个文本视图或图像视图。在实际开发中,还可以在导航栏中使用其他的UI元素来满足设计需求。 以上就是一个简单的线性布局嵌套的实例。通过嵌套不同方向和不同的线性布局,我们可以自由组合和排列界面元素,实现复杂而灵活的界面设计。 ### 回答3: Android Studio是一款用于开发Android应用程序的集成开发环境。在Android Studio中,可以使用各种布局方式来设计应用程序的用户界面。其中,线性布局是一种常用的布局方式,可以实现简单而有效的界面设计。 线性布局是一种按照水平或垂直方向排列子视图的布局方式。当需要在界面中嵌套多个线性布局时,可以灵活地实现各种复杂的布局效果。 假设我们需要设计一个简单的登陆界面,包括一个LOGO图标、用户名文本框、密码文本框和登陆按钮。可以使用线性布局嵌套这些视图。 首先,在布局文件中添加一个垂直方向的线性布局(LinearLayout)作为根布局。在这个布局中,添加一个水平方向的线性布局作为标题栏,放置LOGO图标和应用程序名称。然后,再添加一个垂直方向的线性布局作为内容区域,放置用户名文本框、密码文本框和登陆按钮。 在使用线性布局嵌套时,需要注意设置好各个视图的布局属性,如宽度、高度、权重等,以实现所需的布局效果。可以使用Android Studio提供的可视化布局编辑器和属性面板来方便地设置布局属性。 总之,Android Studio中线性布局嵌套可以实现复杂的界面设计。通过合理地设置各个视图的布局属性,可以实现各种灵活的布局效果,满足不同应用程序的需求。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值