How to correctly override onLayout Method in a Viewgroup class

假装没事ソ 提交于 2019-12-11 12:19:27

问题


I have a class extending from a view group class. Now I know that in onLayout you have to call the layout method on each children. The question here is what values should be passed to the child layout.

In my view I inflate a xml and attach it to this class (The width and height are defined in xml). In onlayout I get one child count which is correct but the width and height of the child is returning 0. How I can get the width and height of the child.

And if I am setting the position and measurement in then what is the onmeasure method is for? I thought we were suppose to measure the child in there.

Below is some source code for reference.

public BaseWidget(Context context, int resourceId) {
    super(context);
    initWithResourceID(resourceId);
}

private void initWithResourceID(int resourceId) {
    inflate(getContext(), resourceId, this);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    final int count = getChildCount();
    for (int i = 0; i < count; i++) {
        View child = getChildAt(i);
        getChildAt(i).layout(l, t, r, b);
//      getChildAt(i).layout((int)child.getX(), (int)(child.getY() + child.getMeasuredHeight()), (int) (child.getX() + child.getMeasuredWidth()), (int)child.getY());
    }
}

The xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="150dp"
android:layout_height="90dp"
android:background="@color/ripple_material_light"
android:orientation="vertical"
android:padding="20dp">

<RelativeLayout
    android:id="@+id/texts"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="1" />

    <TextView
        android:id="@+id/asideTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="2" />
</RelativeLayout>

<View
    android:id="@+id/detailedView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

</View>
</LinearLayout>

When I call getChildAt(i).layout(l, t, r, b); the child is drawn on the whole parent.

When I call

getChildAt(i).layout((int)child.getX(), (int)(child.getY() + 
child.getMeasuredHeight()), (int) (child.getX() + 
child.getMeasuredWidth()), (int)child.getY());

nothing is drawn

So basically I want to know what should we pass to the child.. and what is the major difference between onmeasure and onlayout. I hope I make my question clear.


回答1:


You have to measure the children before trying to get its height and width. And in onLayout, you are decide the position of the child. After measuring, you will have the height and width of all children, then with the help of it, you can position the children. You can set its left, right, top, bottom points based on it height and width. For example, if you want to position the child at (0,0), then in onLayout

child.layout(0,0,child.getMeasuredWidth(), child.getMeasuredHeight());

If right and bottom points are mentioned wrong, the the view may cut/enlarge/shrink.



来源:https://stackoverflow.com/questions/32219784/how-to-correctly-override-onlayout-method-in-a-viewgroup-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!