我需要一个 FrameLayout 来忽略孩子的测量

发布于 2024-10-18 21:11:45 字数 2057 浏览 9 评论 0原文

我有这个 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>

我只希望 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 ) );
    }
}

}

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

感谢您的任何意见。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回忆追雨的时光 2024-10-25 21:11:45

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

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

み格子的夏天 2024-10-25 21:11:45

在我看来,一个更简单的解决方案是直接使用

android:layout_width="100dp"

其中 100 是图像的中等分辨率版本的宽度来确定背景大小和代码到父布局的宽度中。硬编码方法对您不起作用有什么原因吗?

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?

绝影如岚 2024-10-25 21:11:45

尝试这样:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文