Вопрос

I have this FrameLayout:

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/searchresult_picture_layout"
    android:background="@drawable/img_bkgd_results_patch">

    <ProgressBar
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_gravity="center"
        android:id="@+id/searchresult_progressbar"
        android:indeterminate="true"
        android:visibility="gone"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/searchresult_picture"
        android:layout_gravity="center"
        android:scaleType="centerCrop"/>
</FrameLayout>

And I only want the FrameLayout to be as big as the background I'm passing it. If the ImageView is larger than the background for FrameLayout, I want the ImageView to be scaled to fit, not stretch the layout.

I attempted to make a custom layout, but I think I'm really. Here's where I got to:

public class PolaroidFrameLayout extends FrameLayout {

public PolaroidFrameLayout(Context context) {

    super( context );
}

public PolaroidFrameLayout(Context context, AttributeSet attrs) {

    super( context, attrs );
}

public PolaroidFrameLayout(Context context, AttributeSet attrs, int defStyle) {

    super( context, attrs, defStyle );
}

@Override
protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec ) {

    Drawable background = getBackground();
    int backgroundWidth = background.getMinimumWidth();
    int backgroundHeight = background.getMinimumHeight();

    setMeasuredDimension( backgroundWidth, backgroundHeight );

    for (int i = 0; i < getChildCount(); i++) {

        View v = getChildAt( i );
        int viewWidth = v.getWidth();
        int viewHeight = v.getHeight();

        v.measure( MeasureSpec.makeMeasureSpec( Math.min( backgroundWidth, viewWidth ), MeasureSpec.AT_MOST ), MeasureSpec.makeMeasureSpec( Math.min( backgroundHeight, viewHeight ), MeasureSpec.AT_MOST ) );
    }
}

}

The layout stays the size I want, but the children are never drawn. What else do I need to override for this to work?

Thanks for any input.

Это было полезно?

Решение

In onMeasure() you need to measure the children yourself. You must call measure() on each child.

Другие советы

It seems to me that a simpler solution would be to determine the background size and code that in to the width of the parent layout directly using

android:layout_width="100dp"

Where 100 is the width of your medium res version of the image. Is there a reason the hard-coding approach wouldn't work for you?

try it like this:

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/searchresult_picture_layout">

<!-- background layer -->
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/img_bkgd_results_patch">

<ProgressBar
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_gravity="center"
    android:id="@+id/searchresult_progressbar"
    android:indeterminate="true"
    android:visibility="gone"/>

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/searchresult_picture"
    android:layout_gravity="center"
    android:scaleType="centerCrop"/>

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top