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

时间:2015-09-04 11:51:25

标签: android android-resources android-percent-library

新的百分比支持库似乎已发布,但不允许在维度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="@percent/myWidthPercent"
     app:layout_heightPercent="@percent/my/HeightPercent"
     app:layout_marginTopPercent="@percent/myMarginTophPercent"
     app:layout_marginLeftPercent="@percent/myMarginLeftPercent"/>

能够编写类似的代码:

function shorten($inputArray, $path = null, $separator = ".")
{

  $data = array();
  if (!is_null($path)) {
        $path = $path . $separator;
  }

  if (is_array($inputArray)) {
    foreach ($inputArray as $key => &$value) {
        if (!is_array($value)) {
            $data[$path . $key] = $value;
        } else {
            $data = array_merge($data, shorten($value, $path . $key, $separator));
        }
    }
  }

  return $data;
}

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

我错了(我是否错过了其他名字)或是否是我们可以发送给谷歌的功能请求?

1 个答案:

答案 0 :(得分: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"/>