Missing XML resource type for PercentRelativeLayout Percentage value?

放肆的年华 提交于 2019-12-21 07:05:30

问题


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:


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


来源:https://stackoverflow.com/questions/32397379/missing-xml-resource-type-for-percentrelativelayout-percentage-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!