只有背景的空心视图?如何调整自定义视图的大小?

时间:2012-05-17 19:04:12

标签: android view resize

我已经使用唯一的onMeasure()覆盖了我的自定义视图:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension((int)(iwidth * cellWidth), (int)(iheight * cellHeight));
}

使用的变量是类成员,由一个名为setStep()的单个setter设置。

我用按钮更改它:

button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            float step = hidingPanel.getStep();

            step += stepstep;

            hidingPanel.setStep(step);
            hidingPanel.invalidate();

            setTextButton();
        }

    });

布局如下

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<com.inthemoon.incubation.HidingPanel
    android:id="@+id/hidingPanel"
    android:background="@android:color/darker_gray"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="0dp"
    android:layout_marginLeft="0dp"
    />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:background="@android:color/black" 
    >
    <Button
        android:id="@+id/button"
        android:layout_width="200dp"
        android:layout_height="100dp" 
        />    
</LinearLayout>



</RelativeLayout>

其中HidingPanel是我的自定义视图。

我期待按钮按下时我的视图会增长,但事实并非如此。我发现当尺寸为零时,onMeasure()在开始时只被调用几次。如何在按钮点击后推广测量?

1 个答案:

答案 0 :(得分:1)

你需要调用requestLayout();在视图的父级上。

相关问题