Change height percentage based on another view's height in ConstraintLayout

隐身守侯 提交于 2021-02-11 16:14:20

问题


I have a view defined as

  <View
        android:id="@+id/guideLayout"
        android:visibility="invisible"
        app:layout_constraintBottom_toTopOf="@+id/signupBtn"
        app:layout_constraintTop_toBottomOf="@+id/balanceText"
        android:layout_width="0dp"
        android:layout_height="0dp"/>

The view basically extends itself above a signupBtn and below a balanceText

I want to add a new textView whose height is equal to a percentage of its view.

Currently I am doing this

<TextView
        android:layout_marginTop="21dp"
        app:layout_constraintTop_toBottomOf="@+id/balanceText"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintWidth_default="percent"
        app:layout_constraintWidth_percent="0.6"
        app:layout_constraintHeight_default="percent"
        app:layout_constraintHeight_percent="0.08"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:id="@+id/homeBtn"
        android:paddingLeft="60dp"
        android:fontFamily="@font/sanfrancisco_display_regular"
        android:gravity="center_vertical"
        android:textSize="18sp"
        android:text="@string/home"
        android:textColor="@android:color/black"
        />

However I want to set the height of this app:layout_constraintHeight_percent="0.08" based on the above view instead of screen width


回答1:


You can divide the space 80%/20% by using "weighted chains" and ConstraintLayout. The idea is to take the two views you want to occupy a space based on percentages, set contraints to MATCH_CONSTRAINTS and set the weights appropriately using app:layout_constraintVertical_weight as follows. I have given background colors to views so they appear on screen. You will also want to consider setting the views to Space.

activity_main

<android.support.constraint.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/signupBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/balanceText"
        android:layout_width="wrap_content"
        android:layout_height="15dp"
        android:layout_marginTop="16dp"
        android:text="Balance text"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/signupBtn" />

    <View
        android:id="@+id/space"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_red_light"
        app:layout_constraintBottom_toTopOf="@+id/guideLayout"
        app:layout_constraintEnd_toEndOf="@id/signupBtn"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="@id/signupBtn"
        app:layout_constraintTop_toTopOf="@id/signupBtn"
        app:layout_constraintVertical_weight="0.2" />

    <View
        android:id="@+id/guideLayout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_green_light"
        app:layout_constraintBottom_toBottomOf="@id/balanceText"
        app:layout_constraintEnd_toEndOf="@id/signupBtn"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="@id/signupBtn"
        app:layout_constraintTop_toBottomOf="@+id/space"
        app:layout_constraintVertical_weight="0.8" />

</android.support.constraint.ConstraintLayout>


来源:https://stackoverflow.com/questions/52922452/change-height-percentage-based-on-another-views-height-in-constraintlayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!