我需要一个 FrameLayout 来忽略孩子的测量
我有这个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 onMeasure() 中,您需要自己测量孩子。您必须对每个孩子调用measure()。
In onMeasure() you need to measure the children yourself. You must call measure() on each child.
在我看来,一个更简单的解决方案是直接使用
其中 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
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: