FrameLayout extension, child views will no longer be displayed

This is my code with FrameLayout :

 <FrameLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/frameView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/image1" /> </FrameLayout> 

ImageView shows well.

Now I have a custom layout extending from FrameLayout, for example. MyFrameLayout.

In MyFrameLayout, I want the layout height to always be half the width, so my code is:

 public class MyFrameLayout extends FrameLayout { // 3 constructors just call super @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = MeasureSpec.getSize(widthMeasureSpec); int height = (int) (width * 0.5); setMeasuredDimension(width, height); } } 

Then use it in xml:

 <com.test.MyFrameLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/frameView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/image1" /> </com.test.MyFrameLayout> 

But now the internal ImageView is gone.

I think that I am not implementing onMeasure or onLayout correctly. I guess I also need to resize the views of children. But I do not know what to do now.


UPDATE

In TheDimasig's comment , I just checked my code and updated my question. Thanks

+7
source share
3 answers

The easiest way is to change measurepec in onMeasure, but still call super:

  @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = MeasureSpec.getSize(widthMeasureSpec); int height = (int) (width * 0.5); heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } 

This way you get the dimensions you need, but you don't have to manually measure them.

+14
source

You do not see child views because you are not overriding the onMeasure method correctly. You can let the super class implement the logic (this is how the child views will be shown), and then reset the calculated width and height for MyFrameLayout :

 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // let the super class handle calculations super.onMeasure(widthMeasureSpec, heightMeasureSpec); // set again the dimensions, this time calculated as we want setMeasuredDimension(getMeasuredWidth(), getMeasuredHeight() / 2); // this is required because the children keep the super class calculated dimensions (which will not work with the new MyFrameLayout sizes) final int count = getChildCount(); for (int i = 0; i < count; i++) { final View v = getChildAt(i); // this works because you set the dimensions of the ImageView to FILL_PARENT v.measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec( getMeasuredHeight(), MeasureSpec.EXACTLY)); } } 

If this layout you posted is not your complete layout, then my code may not work.

+7
source

I believe that in your onMeasure method onMeasure you forgot to call onMeasure for all child views, including ImageView. You should go through all of your child views and call onMeasure on them something like this:

  for (int i = 0; i < getChildCount(); i++) { getChildAt(i).measure(....); } 
+2
source

All Articles