缺少PercentRelativeLayout百分比值的XML资源类型?

15
新的百分比支持库发布时似乎没有允许在尺寸XML文件中引用百分比值。换句话说,使用:
 <android.support.percent.PercentRelativeLayout
         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_widthPercent="50%"
         app:layout_heightPercent="50%"
         app:layout_marginTopPercent="25%"
         app:layout_marginLeftPercent="25%"/>
 </android.support.percent.PercentFrameLayout/>

能够编写这样的代码:

 <android.support.percent.PercentRelativeLayout
     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_widthPercent="@percent/myWidthPercent"
     app:layout_heightPercent="@percent/my/HeightPercent"
     app:layout_marginTopPercent="@percent/myMarginTophPercent"
     app:layout_marginLeftPercent="@percent/myMarginLeftPercent"/>

其中myWidthPercent在资源文件中定义。

我错了吗(我是否用其他名称错过了它),或者这是一个我们可以向Google发送的功能请求?


你怎么使用这些标签?因为我在第一行<android.support.percent.PercentFrameLayout处出现了错误。 - Pankaj Nimgade
1个回答

29

使用分数代替百分数

<resources>
    <fraction name="myWidthPercent">50%</fraction>
...
</resources>

并且引用它

<ImageView
    app:layout_widthPercent="@fraction/myWidthPercent"
    app:layout_heightPercent="@fraction/my/HeightPercent"
    app:layout_marginTopPercent="@fraction/myMarginTophPercent"
    app:layout_marginLeftPercent="@fraction/myMarginLeftPercent"/>

明白了,正是我所需要的。谢谢Llya。 - u2gilles

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接