水平LinearLayout中的垂直线性布局膨胀返回宽度和高度0

时间:2015-05-12 16:00:27

标签: java android android-linearlayout android-custom-view layout-inflater

我正在尝试在一个自定义水平LinearLayout中充气5 {垂直LinearLayout

首先,我使用View将xml转换为LayoutInflater。然后我将垂直布局添加到水平布局。

问题在于onMeasure,我尝试将子项的宽度和高度设置为LinearLayout,但是当我要求getWidthgetHeight时它总是返回0。

我真的不明白这种行为。

你能帮我吗?非常感谢你。

垂直LinearLayout的XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#000000" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:text="Button"
        android:layout_weight="1" />

    <com.vuric.nativesampler.customviews.BandSeekBar
        android:id="@+id/bandSeek"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="8" 
        android:layout_gravity="center"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="Button" />

</LinearLayout>

容器的类水平LinearLayout:

public class EqualizerCustomLayout extends LinearLayout {

    private static final int MARGIN = 4;
    private Context context;

    public EqualizerCustomLayout(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public EqualizerCustomLayout(Context context, AttributeSet attrs,
            int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }

    public EqualizerCustomLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    private void init() {

        setOrientation(LinearLayout.HORIZONTAL);
        setLayoutParams(new LinearLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        //getNumberOfBands is 5
        for (int i = 0; i < CustomApplicationClass.get().getNumberOfBands(); ++i) {

            LinearLayout linearLayout = (LinearLayout) inflate(context,
                    R.layout.slot_band_layout, null);

            addView(linearLayout);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        Log.v("jajaja", "onMeasure children are " + getChildCount());

        int width = (CustomApplicationClass.get().getScreenSize().x / 100 * 45)
                + MARGIN * 2;
        int height = (CustomApplicationClass.get().getScreenSize().x / 100 * 45)
                + MARGIN * 2;

        LayoutParams params = new LinearLayout.LayoutParams(width / 5, width);

        for (int i = 0; i < getChildCount(); ++i) {

            LinearLayout layout = (LinearLayout) getChildAt(i);
            layout.setLayoutParams(new LinearLayout.LayoutParams(width
                    / getChildCount(), height));

            Log.v("jajaja",
                    "view" + i + " , w: " + layout.getWidth() + " , h: "
                            + layout.getHeight()); // always return 0 for both width and height
        }

        setMeasuredDimension(width, height);
    }
}

1 个答案:

答案 0 :(得分:1)

您必须等到实际绘制视图。有一种解决方法可以找到here