I have a layer-list within a file 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>
This is used in a customized 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>
which itself is used e.g. in
<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>
Additionally, I have the CustomView.java class to edit the behavior of the customView (and especially the layers).
In there, I am trying to achieve an update of the size of the layer-list depending on the size of the customView (and a parameter 'ratio'). I tried it like this:
// ...
@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();
}
}
So, that was lots of code... Unfortunately, this does not work properly. If the frame layout is loaded the first time, I can only see a very small rectangle.
Only if I switch back and forth to this view, the size is applied as I wanted to:
Does anybody have ideas?