Missing XML resource type for PercentRelativeLayou

2019-04-04 06:46发布

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?

1条回答
手持菜刀,她持情操
2楼-- · 2019-04-04 06:51

Use fraction instead of percent

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

And referencing to it

<ImageView
    app:layout_widthPercent="@fraction/myWidthPercent"
    app:layout_heightPercent="@fraction/my/HeightPercent"
    app:layout_marginTopPercent="@fraction/myMarginTophPercent"
    app:layout_marginLeftPercent="@fraction/myMarginLeftPercent"/>
查看更多
登录 后发表回答