今天先要解决的问题是怎么样点击按钮来隐藏掉页面的部分布局。
用的是 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】效果图
点击按钮后下面的布局隐藏了