如何将约束点视图设置为百分比最小的高度

发布于 2025-01-30 17:23:34 字数 2043 浏览 3 评论 0原文

我使用的是约束layout,我想创建一个文本视图,该文本视图占屏幕高度的30%或内容的大小,以更大的为准。

我可以使用约束来使其通过水平准则占屏幕的30%,并且可以删除这些约束,以使其随文本的大小而生长/收缩。但是,我不能同时做这两个事情 - 也就是说,最低限度的30%,但在需要的话之外生长。

我尝试使用layout_constraintheight_min,并将其设置为一个百分比,为指南,以及具有设定高度的视图,但这些都没有。我知道,如果我在200DP之类的硬编码值中投入了数字值,但是我希望该值是比例的 - 也就是说,百分比。

我本质上想要这样的东西,尽管正如我上面提到的那样,该代码不起作用,因为从技术上讲,指南的高度为0:

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintHeight_min="@+id/guideline8"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:text="MyText" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline8"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.7" />

我也尝试过

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintHeight_min="@+id/heightGuide"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:text="MyText" />

    <View
        android:id="@+id/heightGuide"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="@+id/guideline8"
        app:layout_constraintBottom_toBottomOf="parent"
        android:visibility="invisible"/>
        
    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline8"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.7" />

,但这也不起作用。

我如何使视图具有最低高度,而高度是父母的一部分,但是如果需要,它也可以超过该高度?

I am using a ConstraintLayout and I want to create a TextView that takes up 30% of the height of the screen or the size of its content, whichever is greater.

I can use constraints to make it take up 30% of the screen via a horizontal guideline, and I can leave off those constraints to make it grow/shrink with the size of the text. However, I can't get it to do both --- that is, be 30% as a minimum but grow beyond that if needed.

I tried using layout_constraintHeight_min and setting it to a percent, to a guideline, and to a view with a set height, but none of those works. I know it will work if I put in a hardcoded value like 200dp, but I want the value to be proportional --- that is, a percent.

I essentially want something like this, though as I mentioned above, this code doesn't work because technically a guideline has a height of 0:

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintHeight_min="@+id/guideline8"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:text="MyText" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline8"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.7" />

I also tried

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintHeight_min="@+id/heightGuide"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:text="MyText" />

    <View
        android:id="@+id/heightGuide"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="@+id/guideline8"
        app:layout_constraintBottom_toBottomOf="parent"
        android:visibility="invisible"/>
        
    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline8"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.7" />

but that doesn't work either.

How can I make a view have a minimum height that is a proportion of the parent, but also allow it to grow beyond that height if needed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新人笑 2025-02-06 17:23:34

我必须处理类似的情况,并最终通过将以下内容添加到我的观点来解决它:

    android:layout_height="0dp"
    app:layout_constraintHeight_min="wrap"
    app:layout_constraintHeight_percent="0.66"

希望这对您有用

I had to deal with a similar situation and finally managed to solve it by adding the following to my view:

    android:layout_height="0dp"
    app:layout_constraintHeight_min="wrap"
    app:layout_constraintHeight_percent="0.66"

Hope this works for you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文