1

在创建自定义 ViewGroup 时,我遇到了问题。我使用以下代码创建了一个 ViewGroup:

import android.content.Context;
import android.graphics.RectF;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

public class noteLayout extends ViewGroup {
    float leftOrientationSize = 0;
    float rightOrientationSize=0;
    public noteLayout(Context activityContext)
    {
        super(activityContext);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        RelativeLayout mainParent = (RelativeLayout) getParent();
        final RelativeLayout child = (RelativeLayout) getChildAt(0);
        for (int i = 0; i < child.getChildCount(); i++) {
            final View childOfChild = child.getChildAt(i);
            if (childOfChild != null) {
                childOfChild.measure(MeasureSpec.makeMeasureSpec((mainParent.getWidth() / 2) - 30, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(mainParent.getHeight(), MeasureSpec.AT_MOST));
            }
        }
        child.measure(MeasureSpec.makeMeasureSpec((mainParent.getWidth() / 2) - 30, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec((mainParent.getHeight() / 2) - 300, MeasureSpec.AT_MOST));
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int numberOfChild = getChildCount();
        for (int i = 0;i<numberOfChild;i++){
            View childView = getChildAt(i);
            float childHeight = (float) childView.getMeasuredHeight();
            float childWidth = (float) childView.getMeasuredWidth();
            RectF rect = new RectF();
            rect.bottom = childHeight+20;
            rect.top = 20;
            rect.left = 20;
            rect.right = childWidth+20;
            childView.layout((int) rect.left, (int) rect.top, (int) rect.right, (int) rect.bottom);
        }
    }
}

noteLayout customViewgroup = new noteLayout();所以我基本上通过Afterward, do实例化我的 ViewGroup 的一个新实例customViewgroup.addView(getLayoutInflater().inflate(R.Layout.test3,null));将 test3 xml 布局添加到我的 CustomViewGroup 中。然后我获得了对我的主要活动相对布局的引用,并使用addView(customViewGroup)我的主要活动相对布局的方法添加到包含 test3 布局的 CustomViewGroup 中。

我的 test3 布局如图:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:background="@android:color/holo_blue_bright"
    android:id="@+id/ma"
    android:elevation="5dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Note"
        android:id="@+id/displayNote"
        android:paddingBottom="10dp"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:ellipsize="end"
        android:layout_below="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Title:"
        android:id="@+id/textView2"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"/>

</RelativeLayout>

我的问题是我不明白我的 ViewGroup 如何在它包含的相对布局中测量 TextView,所以这里有一些图像可以解释我的担忧。

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

所以上图的顺序如下。带有完整青色背景的图像是我的 xml 布局中 test3 的图像,第二个是它的实际呈现方式。我担心的是我不知道到底发生了什么,因为图像呈现的方式与我在 xml 布局中的方式不匹配。有时注释文本会出现在中间,有时它会消失,具体取决于它在相对布局上的位置。但是,我担心的是,与 xml 设计选项卡中的注释视图相比,为什么呈现如此不同的注释视图?也许我的 ViewGroup 可能编码错误?

4

0 回答 0