PercentFrameLayout:您必须提供layout_width属性
问题描述:
我尝试使用 PercentFrameLayout ,但收到以下错误消息:>
I try to use a PercentFrameLayout but I get this error message:
Binary XML file line #: You must supply a layout_width attribute.
这是我使用的xml:
<android.support.percent.PercentFrameLayout
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">
<ImageView
app:layout_heightPercent="50%"
app:layout_marginLeftPercent="25%"
app:layout_marginTopPercent="25%"
app:layout_widthPercent="50%"/>
</android.support.percent.PercentFrameLayout>
这是错误还是我的错误?
Is this a bug or is it my mistake?
答
由于XML布局规范,仍然需要layout_width
和layout_height
属性,但是在PercentFrameLayout
或PercentRelativeLayout
中实际上将忽略它们.
The layout_width
and layout_height
attributes are still required due to the XML layout specifications, but are effectively ignored in a PercentFrameLayout
or PercentRelativeLayout
.
您只需要将值设置为0dp
或wrap_content
:
You just have to set the values to 0dp
or wrap_content
:
<ImageView
android:layout_height="0dp"
android:layout_width="0dp"
app:layout_heightPercent="50%"
app:layout_marginLeftPercent="25%"
app:layout_marginTopPercent="25%"
app:layout_widthPercent="50%" />
这与标准LinearLayout
和layout_weight
的用法类似,在该情况下,您也必须指定layout_width
.
This is similar to the usage of a standard LinearLayout
with layout_weight
where you have to specify a layout_width
too.