我有这个框架布局:
<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 ) );
}
}
}
布局保持我想要的大小,但永远不会绘制孩子。我还需要覆盖什么才能使其正常工作?
感谢您的任何意见。