6

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?

Ralph Bergmann
  • 3,015
  • 4
  • 30
  • 61
  • That looks exactly like the example at http://developer.android.com/reference/android/support/percent/PercentFrameLayout.html other than the order of parameters so I assume it is correct. Are you sure those are the views with the problem? If you remove those views does your layout load correctly? – Cory Charlton Jan 21 '16 at 18:17
  • @CoryCharlton yes it is from the doc and yes it is the view with the problem. Android Studio highlights it as a problem too. – Ralph Bergmann Jan 22 '16 at 18:37

1 Answers1

4

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.

Floern
  • 33,559
  • 24
  • 104
  • 119
  • But the doc says `It is not necessary to specify layout_width/height if you specify layout_widthPercent.` – Ralph Bergmann Jan 22 '16 at 18:38
  • @RalphBergmann That's weird, I couldn't even compile the XML without specifying the width/height.. So I'm not sure whether the docs are correct. – Floern Jan 22 '16 at 18:47
  • Looks like it IS necessary-- I've tried ommitting width/height (no exceptions thrown), and can only get it to render properly by setting both values to 0dp. – Rachael Nov 16 '16 at 00:30