ConstraintLayout - 在另一个视图上方查看时,constraintHeight_percent会被忽略吗?

时间:2018-04-05 07:11:29

标签: android android-constraintlayout

]我在版本1.1.0 beta 6中使用约束布局(顺便说一句,这比稳定的1.0.2更好......)

但是,在某些情况下,使用constraintHeight_percent并不适合我。

这就是我想要实现的目标:

  1. 在约束布局
  2. 的底部放置一个固定高度的按钮(底部按钮)
  3. 在约束布局的顶部和上方的底部按钮之间放置另一个按钮(顶部按钮)。
  4. 将顶部按钮的最终高度设为其高度的一半。
  5. 这里是xml:

    <?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="300dp"
        android:background="#71B9DE">
    
        <Button
            android:id="@+id/bottomButton"
            android:layout_width="0dp"
            android:layout_height="150dp"
            android:text="BOTTOM BUTTON"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>
    
        <Button
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:text="TOP BUTTON - The height should be 75dp!!"
            app:layout_constraintBottom_toTopOf="@id/bottomButton"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHeight_percent="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    
    </android.support.constraint.ConstraintLayout>
    

    这是预览:

    enter image description here

    以下是我的预期:

    enter image description here

    我不明白为什么在使用 app:layout_constraintHeight_percent =&#34; 0.5&#34; 后,顶部按钮的高度仍然是150DP而不是75DP。

    似乎 app:layout_constraintHeight_percent =&#34; 0.5&#34; 计算与父高相关,并且在受约束后与按钮高度本身无关。< / p>

    顺便说一下,我没有寻找其他解决方案(使用指南,障碍)。我真的想弄清楚它为什么不起作用。

    感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

根据{ MATCH_CONSTRAINT尺寸(1.1中添加)'部分中的official reference document

  

layout_constraintWidth_percent和layout_constraintHeight_percent:将此维度的大小设置为父级的百分比

前提是相应的约束设置为MATCH_CONSTRAINT0dp

答案 1 :(得分:0)

你应该使用android:layout_width =“match_parent”而不是0dp