1

我在文件layers.xml中有一个图层列表:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/outer_rect">
    <shape android:shape="rectangle">
        <stroke
                android:width="3dp"
                android:color="@color/gray4"/>
        <solid android:color="@color/white"/>
    </shape>
</item>
<item android:top="10dp" android:bottom="10dp" android:left="10dp" android:right="10dp"
      android:drawable="@drawable/dot_pattern_bitmap"/>
</layer-list>

这在自定义的 FrameLayout customView.xml 中使用:

<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/layers"/>
</merge>

它本身用于例如

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/space_m"
    android:orientation="vertical">

<my.namespace.customView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</FrameLayout>

此外,我有 CustomView.java 类来编辑 customView(尤其是图层)的行为。

在那里,我试图根据 customView 的大小(和参数“比率”)来更新层列表的大小。我试过这样:

// ...
@Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    layoutWidth = MeasureSpec.getSize(widthMeasureSpec);
    layoutHeight = MeasureSpec.getSize(heightMeasureSpec);

    readjustSize(ratio);
}

private void readjustSize(final double ratio) {
    if (layoutHeight != 0 && layoutWidth != 0) {
        // determine desired width and height using the layout width
        int desiredWidth = layoutWidth;
        int desiredHeight = (int) (layoutWidth / ratio);

        // if it does not fit, adjust using the layout height
        if (desiredHeight > layoutHeight) {
            desiredHeight = layoutHeight;
            desiredWidth = (int) (layoutHeight * ratio);
        }

        // get the basic layer
        final LayerDrawable ld =
            (LayerDrawable) ContextCompat.getDrawable(getContext(), R.layers);
        final GradientDrawable outerRectShape =
            (GradientDrawable) ld.findDrawableByLayerId(R.id.outer_rect);

        // and adapt the size
        outerRectShape.setSize(desiredWidth, desiredHeight);
        outerRectShape.setBounds(0, 0, desiredWidth, desiredHeight);

        // redraw
        invalidate();
        requestLayout();
    }
}

所以,这是很多代码......不幸的是,这不能正常工作。如果第一次加载框架布局,我只能看到一个非常小的矩形。

只有当我来回切换到此视图时,才会按照我的意愿应用大小:

有人有想法吗?

4

1 回答 1

0

最后,我自己找到了答案。:) 实际上,这很简单。当我掌握可绘制的图层时

final LayerDrawable ld =
    (LayerDrawable) ContextCompat.getDrawable(getContext(), R.layers);

我只是获得一般的资源,而不是我的可绘制图像的具体 src。当我将代码修改为

final View root = inflate(getContext(), R.layout.custom_view, this);
final ImageView imageView = (ImageView) root.findViewById(R.id.layers);
final LayerDrawable ld = (LayerDrawable) imageView.getDrawable();

在 CustomView 初始化并为 ImageView 提供 id“层”时,它按预期工作。

此外,我不得不更改 onMeasure 中调用方法的顺序:

@Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
    layoutWidth = MeasureSpec.getSize(widthMeasureSpec);
    layoutHeight = MeasureSpec.getSize(heightMeasureSpec);

    readjustSize(ratio);

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
} 

编辑:这仅适用于 Android 版本 >= 6.0。对于旧版本,我仍然遇到问题中描述的相同问题。

于 2016-10-19T07:36:20.617 回答