Search code examples
androidandroid-resourcesandroid-percent-library

Missing XML resource type for PercentRelativeLayout Percentage value?


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?


Solution

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