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