[英]Change height percentage based on another view's height in 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 该视图基本上将自身扩展到signupBtn之上和balanceText之下
I want to add a new textView whose height is equal to a percentage of its view. 我想添加一个新的textView,其高度等于其视图的百分比。
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 但是我想基于上面的视图而不是屏幕宽度来设置此app:layout_constraintHeight_percent="0.08"
的高度app:layout_constraintHeight_percent="0.08"
You can divide the space 80%/20% by using "weighted chains" and ConstraintLayout
. 您可以使用“加权链”和ConstraintLayout
划分80%/ 20%的空间。 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. 想法是根据百分比获取要占用空间的两个视图,将MATCH_CONSTRAINTS
设置为MATCH_CONSTRAINTS
并使用app:layout_constraintVertical_weight
适当地设置权重,如下所示。 I have given background colors to views so they appear on screen. 我给视图赋予了背景色,以便它们出现在屏幕上。 You will also want to consider setting the views to Space
. 您还需要考虑将视图设置为Space
。
activity_main 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.