7

有一个问题How to make ConstraintLayout work with percentage values?及其答案演示如何使用百分比:的Android - ConstraintLayout使用百分比梦诗

<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"/> 

但是,如果你不想硬编码的百分比,但使用它不是一个扪资源工作。

<!-- 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).

你怎么设置没有将值硬编码到布局中的百分比?

回答

7

而不是使用一个扪资源的使用类型扪的项目资源:如果使用整数

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

,一个integer资源将工作最好:

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

+1

如果这个作品,那么这就是IDE错误或构建工具错误,因为两种方法都会产生维度资源。 – CommonsWare

+1

将资源定义为维度需要一个度量单位。将它定义为“dimen”类型的项目不会。另外可以添加格式“float”,但不是必需的。 – neits

+0

啊,我明白了。尝试使用“float”或“integer”资源,然后尝试使用“dimen”。 – CommonsWare