0

我想使用扩展 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());
    }
}

问题是它根本不显示子布局,那么我该如何完成这项工作?谢谢!

(对不起,我的英语不好)

编辑:我用过这个类https://github.com/jesperborgstrup/buzzingandroid/blob/master/src/com/buzzingandroid/ui/ViewAspectRatioMeasurer.java

4

1 回答 1

0

错误是您正在使用android:orientation="horizontal"for CustomLayout(which extends LinearLayout) 和android:layout_width="match_parent"for your child LinearLayouts.

尝试将您的布局更改为:

<com.myPackage.CustomLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        layout_weight="1" >
        .....
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        layout_weight="1">
        .....
    </LinearLayout>

</com.myPackage.CustomLayout>

编辑:LinearLayout此外,如果您CustomLayout的代码是您共享的,我也看不出有任何理由使用子类。

于 2013-11-21T17:13:32.123 回答