如何正确重写Viewgroup类中的onLayout方法 [英] How to correctly override onLayout Method in a Viewgroup class

查看:99
本文介绍了如何正确重写Viewgroup类中的onLayout方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从视图组类扩展的类.现在我知道在onLayout中,您必须在每个子级上调用layout方法.这里的问题是应该将哪些值传递给子布局.

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.

在我看来,我将xml膨胀并将其附加到此类(宽度和高度在xml中定义).在布局中,我得到一个正确的子计数,但该子的宽度和高度返回0.如何获得该子的宽度和高度.

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.

如果我要设置位置和测量值,那么onmeasure方法是做什么用的?我以为我们应该在那儿测量孩子.

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());
    }
}

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>

当我打电话给getChildAt(i).layout(l, t, r, b);时,孩子被整个父母吸引.

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

当我打电话

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

什么都没画

所以基本上我想知道我们应该把什么传给孩子.. onmeasure和onlayout之间的主要区别是什么.我希望我能澄清我的问题.

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.

推荐答案

您必须先测量孩子,然后才能获取其高度和宽度.在onLayout中,您可以确定子项的位置. 测量后,您将获得所有子代的高度和宽度,然后在其帮助下,可以将子代定位.您可以根据其高度和宽度设置其左,右,上,下点. 例如,如果要将子级放置在(0,0)处,则在onLayout

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.

这篇关于如何正确重写Viewgroup类中的onLayout方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆