压倒OnMeasure

时间:2014-01-10 09:42:08

标签: android layout view android-custom-view onmeasure

我使用以下xml

制作了自定义视图
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/packetlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<View
    android:id="@+id/view2"
    android:layout_width="1dip"
    android:layout_height="fill_parent"
    android:background="#FF0000FF" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/tempEdit"
    android:layout_marginLeft="10dp"
    android:layout_toRightOf="@+id/view2"
    android:text="Temp : "
    android:textAppearance="?android:attr/textAppearanceSmall" />

<EditText
    android:id="@+id/timeEdit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/tempEdit"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/drawingCanvas"
    android:ems="5"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<EditText
    android:id="@+id/tempEdit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/textView1"
    android:ems="5"
    android:text="115"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:width="50dp"
    android:inputType="number" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/timeEdit"
    android:layout_alignBottom="@+id/timeEdit"
    android:layout_alignLeft="@+id/drawingCanvas"
    android:text="Time : "
    android:textAppearance="?android:attr/textAppearanceSmall" />

<LinearLayout
    android:id="@+id/drawingCanvas"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/timeEdit"
    android:layout_alignLeft="@+id/textView1"
    android:layout_alignRight="@+id/tempEdit"
    android:layout_below="@+id/textView1"
    android:orientation="vertical" 
    android:layout_marginRight="15dp">

</LinearLayout>

我想将多个副本插入到带有Linearlayout的ScrollView中。

由于屏幕尺寸不同,我知道我们必须使用onmeasure覆盖相应地缩放它们。

    @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // TODO Auto-generated method stub
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    // get width and height size and mode
    int wSpec = MeasureSpec.getSize(widthMeasureSpec);

    int hSpec = MeasureSpec.getSize(heightMeasureSpec);
    int hMode = MeasureSpec.getMode(heightMeasureSpec);

    int width = wSpec;
    int height = hSpec;

    // ideal height for the number display
    int idealHeight = (int) (wSpec * 1);

    if(idealHeight < hSpec)
    {
        height = idealHeight;
    }

    Log.e("logging " , wSpec + "   " + hSpec + "   " + hMode + "   " + width + "  " + height);
    setMeasuredDimension(wSpec / 2, hSpec/2);
}  

使用上面的代码,我总是得到一个wSpec = 0和hSpec = 382。

如何获取我想将CustomView插入其中的LinearLayout的大小?

由于

0 个答案:

没有答案