PercentFrameLayout: You must supply a layout_width attribute

北慕城南 提交于 2019-12-12 10:49:20

问题


I try to use a PercentFrameLayout but I get this error message:

Binary XML file line #: You must supply a layout_width attribute.

This is the xml I use:

<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?


回答1:


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.

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

This is similar to the usage of a standard LinearLayout with layout_weight where you have to specify a layout_width too.



来源:https://stackoverflow.com/questions/34931023/percentframelayout-you-must-supply-a-layout-width-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!