根据ConstraintLayout中另一个视图的高度更改高度百分比 [英] Change height percentage based on another view's height in ConstraintLayout

查看:136
本文介绍了根据ConstraintLayout中另一个视图的高度更改高度百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个定义为

的视图

 < Viewandroid: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"/> 

该视图基本上将自身扩展到signupBtn之上和balanceText之下

我想添加一个新的textView,其高度等于其视图的百分比.

目前我正在这样做

 < TextViewandroid: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"/> 

但是我想根据上面的视图而不是屏幕宽度来设置此 app:layout_constraintHeight_percent ="0.08" 的高度

解决方案

您可以使用

activity_main

 < android.support.constraint.ConstraintLayoutandroid:layout_width ="match_parent"android:layout_height ="match_parent"><按钮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"/>< TextViewandroid:id ="@ + id/balanceText"android:layout_width ="wrap_content"android:layout_height ="15dp"android:layout_marginTop ="16dp"android:text =平衡文字"app:layout_constraintLeft_toLeftOf ="parent"app:layout_constraintRight_toRightOf ="parent"app:layout_constraintTop_toBottomOf ="@ + id/signupBtn"/><查看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_constraintHorizo​​ntal_bias ="0.5"app:layout_constraintStart_toStartOf ="@ id/signupBtn"app:layout_constraintTop_toTopOf ="@ id/signupBtn"app:layout_constraintVertical_weight ="0.2"/><查看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_constraintHorizo​​ntal_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> 

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

解决方案

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>

这篇关于根据ConstraintLayout中另一个视图的高度更改高度百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆