Android - 使用尺寸的 ConstraintLayout 百分比

2023-12-23

有一个问题如何使 ConstraintLayout 使用百分比值? https://stackoverflow.com/questions/37318228/how-to-make-constraintlayout-work-with-percentage-values及其答案显示了如何使用百分比:

<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="1dp"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.5"/>

但是,如果您不想对百分比进行硬编码,而是使用 dimen 资源,则它不起作用。

<!-- inside the layout -->

<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="1dp"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="@dimen/guideline_perc"/>

<!-- inside dimens.xml -->

<dimen name="guideline_perc>0.5</dimen>

您会收到以下错误:

Float types not allowed (at 'guideline_perc' with value 0.5).

如果将该值替换为 1,则会返回类似的错误:

Integer types not allowed (at 'guideline_perc' with value 1).

如何设置百分比而不将值硬编码到布局中?


不要使用 dimen 资源,而是使用 dimen 类型的项目资源:

<item name="guideline_perc" type="dimen">0.5</item>

如果使用整数,则integer资源效果最好:

<integer name="guideline_perc">1</integer>

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android - 使用尺寸的 ConstraintLayout 百分比 的相关文章

随机推荐