1

我有这个框架布局:

<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>

而且我只希望 FrameLayout 与我传递的背景一样大。如果 ImageView 大于 FrameLayout 的背景,我希望 ImageView 被缩放以适应,而不是拉伸布局。

我试图制作一个自定义布局,但我认为我是真的。这是我要去的地方:

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 ) );
    }
}

}

布局保持我想要的大小,但永远不会绘制孩子。我还需要覆盖什么才能使其正常工作?

感谢您的任何意见。

4

3 回答 3

2

在 onMeasure() 中,您需要自己测量孩子。您必须对每个孩子调用 measure() 。

于 2011-02-22T22:57:49.527 回答
0

试试这样:

<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"/>

于 2011-11-07T08:55:57.077 回答
0

在我看来,一个更简单的解决方案是直接使用确定背景大小和代码到父布局的宽度

android:layout_width="100dp"

其中 100 是图像的中等分辨率版本的宽度。硬编码方法对您不起作用有什么原因吗?

于 2011-02-22T23:36:06.853 回答