15

It seems that the new Percent Support Library was released without allowing to reference a Percentage value in a dimension xml file.

That is, instead of :

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

being able to code something like that :

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

where myWidthPercent is defined in a resource files.

Am i wrong (did i miss it with another name) or is it a feature request that we could send to google?

4

1 回答 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"/>
于 2015-09-04T12:11:54.490 回答