5

我尝试使用PercentFrameLayout但我收到此错误信息:PercentFrameLayout:必须提供layout_width属性

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> 

这是一个错误或这是我的错误吗?

+0

这看起来和http://developer.android.com/reference/android/support/percent/PercentFrameLayout.html中的参数不同,因为我认为它是正确的。你确定这些是对问题的看法吗?如果您删除这些视图,您的布局是否正确加载? –

+0

@CoryCharlton是的,它来自文档,是的,它是与问题的看法。 Android Studio也强调它也是一个问题。 –

回答

4

layout_widthlayout_height属性仍然需要由于XML布局规范,但在PercentFrameLayoutPercentRelativeLayout实际上被忽略。

你只需要值设置为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%" /> 

这是类似的标准LinearLayoutlayout_weight使用,你必须指定一个layout_width了。

+0

但是文档说'如果指定layout_widthPercent,没有必要指定layout_width/height。 –

+0

@RalphBergmann这很奇怪,我甚至无法编译XML而没有指定宽度/高度。所以我不确定是否该文档是正确的。 – Floern

+0

看起来像是必要的 - 我试过忽略宽度/高度(没有例外抛出),并且只能通过将两个值都设置为0dp来正确呈现它。 – Rachael

相关问题