我想使用扩展 LinearLayout 类并覆盖 onMeasure() 方法的自定义布局,如下所示:
<com.myPackage.CustomLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
.....
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
.....
</LinearLayout>
</com.myPackage.CustomLayout>
我的自定义布局:
public class CustomLayout extends LinearLayout {
private static final double VIEW_ASPECT_RATIO = 2;
private ViewAspectRatioMeasurer varm = new ViewAspectRatioMeasurer(
VIEW_ASPECT_RATIO);
public HomeLayout(Context context) {
super(context);
}
public HomeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
varm.measure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(varm.getMeasuredWidth(), varm.getMeasuredHeight());
}
}
问题是它根本不显示子布局,那么我该如何完成这项工作?谢谢!
(对不起,我的英语不好)