PercentFrameLayout:您必须提供layout_width属性

Ral*_*ann 5 android android-layout android-percent-library android-percent-layout

我尝试使用PercentFrameLayout但我收到此错误消息:

Binary XML file line #: You must supply a layout_width attribute.
Run Code Online (Sandbox Code Playgroud)

这是我使用的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>
Run Code Online (Sandbox Code Playgroud)

这是一个错误还是我的错?

Flo*_*ern 4

由于 XML 布局规范,layout_width和属性仍然是必需的,但在 a或中实际上被忽略。layout_heightPercentFrameLayoutPercentRelativeLayout

您只需将值设置为0dpwrap_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%" />
Run Code Online (Sandbox Code Playgroud)

这类似于标准的用法LinearLayoutlayout_weight您必须指定一个layout_width太。