Android-使用调暗的ConstraintLayout百分比 [英] Android - ConstraintLayout percentage using dimens
问题描述
有一个问题如何使ConstraintLayout与百分比值一起使用? 及其答案显示了如何使用百分比:
There is a question How to make ConstraintLayout work with percentage values? and its answers show how to use the percentages:
<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资源,那么它将不起作用.
But if you don't want to hardcode the percentage but use a dimen resource it does not work.
<!-- 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,则会返回类似错误:
If you replace the value with 1, a similar error is returned:
Integer types not allowed (at 'guideline_perc' with value 1).
如何在不将值硬编码到布局中的情况下设置百分比?
How do you set a percentage without hardcoding the value into the layout?
推荐答案
使用dimen类型的项目资源代替使用dimen资源:
Instead of using a dimen resource, use an item resource of type dimen:
<item name="guideline_perc" type="dimen">0.5</item>
如果使用整数,则integer
资源将最有效:
If using integers, an integer
resource would work best:
<integer name="guideline_perc">1</integer>
这篇关于Android-使用调暗的ConstraintLayout百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!