关于ConstraintLayout自适应高度遇到的坑 记录下来
android:layout_height="wrap_content"
为了缩减嵌套层及采用了ConstraintLayout作为dialog布局,但是发现dialog下边的确定按钮总是被盖一部分
线上问题代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="wrap_content"
android:background="@drawable/dialog_bg">
<ImageView
android:id="@+id/type_uploading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="66px"
android:src="@mipmap/success_uploading"
android:visibility="gone"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="80px"
android:layout_marginTop="70px"
android:layout_marginRight="80px"
android:gravity="center"
android:text="上传成功"
android:textColor="@color/widthdrawnoticestr"
android:textSize="17sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/type_uploading" />
<--描述文字默认隐藏-->
<TextView
android:id="@+id/tv_type_summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30px"
android:text="描述文字"
android:visibility="gone"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_type" />
<ImageView
android:id="@+id/img_division"
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginTop="59px"
android:background="@color/division_line"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_type_summary" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/img_division">
<TextView
android:id="@+id/tv_erro_ok"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/ok"
android:textColor="@color/widthdrawnoticestr"
android:textSize="17sp"
android:visibility="gone" />
<--其他布局-->
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
以上代码有一下问题
(1)上下控件衔接并没有连贯 控件没有完全指定上边或下界约束控件
(2)如果出现visible=gone这个情况需要嵌套一个布局 否则约束链断了,同样出现布局问题
(3)这个布局并不复杂使用RelativeLayout完全可以了还可以省略很多代码
修改代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="wrap_content"
android:background="@drawable/dialog_bg">
<LinearLayout
android:id="@+id/ln_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="@+id/img_division"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/type_uploading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="66px"
android:src="@mipmap/success_uploading"
android:visibility="gone" />
<TextView
android:id="@+id/tv_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="80px"
android:layout_marginTop="70px"
android:layout_marginRight="80px"
android:gravity="center"
android:text="@string/article_uploading"
android:textColor="@color/widthdrawnoticestr"
android:textSize="17sp" />
<TextView
android:id="@+id/tv_type_summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30px"
android:text="今日已达上限"
android:visibility="gone" />
</LinearLayout>
<ImageView
android:id="@+id/img_division"
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginTop="59px"
android:background="@color/division_line"
app:layout_constraintBottom_toTopOf="@+id/rl_bootom"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ln_top" />
<RelativeLayout
android:id="@+id/rl_bootom"
android:layout_width="match_parent"
android:layout_height="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/img_division">
<TextView
android:id="@+id/tv_erro_ok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:text="@string/ok"
android:textColor="@color/widthdrawnoticestr"
android:textSize="17sp"
android:visibility="visible" />
<--其他布局-->
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
总结:RelativeLayout 相对位置 left top right bottom 在设置相对属性的时候回失效
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"其他情况没有问题
ConstraintLayout 控件布局不能有断链,否则约束布局会失效