gpt4 book ai didi

android - 将 View 宽度动态设置为父布局的百分比

转载 作者:行者123 更新时间:2023-11-29 15:03:39 25 4
gpt4 key购买 nike

我有一个简单的进度条 View ,我想按父布局的百分比动态更改宽度。

我看到有一个 set minimumWidth api。但是要传入的正确数字是多少?我是否应该获取父级宽度,然后只计算子级的百分比和宽度并将其作为 minimumWidth 传递。

或者,这是否可以仅使用 xml 布局代码实现?

最佳答案

类(class)来自 android.support.percent.*包装如 PercentRelativeLayout , 目前已弃用

This class was deprecated in API level 26.1.0.
consider using ConstraintLayout and associated layouts instead. The following shows how to replicate the functionality of percentage layouts with a ConstraintLayout. The Guidelines are used to define each percentage break point, and then a Button view is stretched to fill the gap:

只需使用 ConstraintLayoutGuideLines

例子:

<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="match_parent">

<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/left_guideline"
app:layout_constraintGuide_percent=".15"
android:orientation="vertical"/>

<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/right_guideline"
app:layout_constraintGuide_percent=".85"
android:orientation="vertical"/>

<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/top_guideline"
app:layout_constraintGuide_percent=".15"
android:orientation="horizontal"/>

<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bottom_guideline"
app:layout_constraintGuide_percent=".85"
android:orientation="horizontal"/>

<Button
android:text="Button"
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/button"
app:layout_constraintLeft_toLeftOf="@+id/left_guideline"
app:layout_constraintRight_toRightOf="@+id/right_guideline"
app:layout_constraintTop_toTopOf="@+id/top_guideline"
app:layout_constraintBottom_toBottomOf="@+id/bottom_guideline" />

</android.support.constraint.ConstraintLayout>

关于android - 将 View 宽度动态设置为父布局的百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40891955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com