【问题标题】:custom view doesn't draw in a custom viewgroup自定义视图不在自定义视图组中绘制
【发布时间】:2012-12-29 18:01:38
【问题描述】:

我正在开发一个测试来尝试了解视图和视图组的行为。我创建了一个非常简单的自定义视图组(以下称为 viewgroup1),我已经正确实现了所有重要的方法(onLayout、onMeasure、onSizeChanged)。然后我创建了一个简单的视图,并将视图从 onclick 事件添加到 viewgroup1。正确显示视图并调用其方法(onSizeChanged、onLayout、onDraw)。

在成功测试之后,我创建了另一个自定义视图组(以下称为 viewgroup2),这次我将 viewgroup2 添加到了 viewgroup1。然后我将自定义视图从 onclick 事件添加到 viewgroup2。我预计一切都会像第一次测试一样发生,但是当我单击 viewgroup2 时,视图被添加到这个视图中,但是只调用了 viewgroup1 方法(onMeasure,onLayout)。调用不会沿继承树传播。

结果是树结构是正确的,但视图没有显示,尽管我已经为每个对象调用了 requestLayout、forcelayout 等。

树形结构为:viewgroup1---viewgroup2---view。

清单代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.yoredevelopment.tests"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="11" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".AndroidTestsActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

布局代码(main.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res/com.yoredevelopment.tests"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ff000000"
    android:orientation="vertical" >
        <com.yoredevelopment.tests.TestViewGroup
                android:id="@+id/TestViewGroup"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="#ff00cccc"
                />
</LinearLayout>

活动代码:

import android.app.Activity;
import android.os.Bundle;

public class AndroidTestsActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

View 和 ViewGroups 代码:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class TestViewGroup extends ViewGroup {
    public TestViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        TestViewChild tvc = new TestViewChild(context);
        tvc.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        tvc.setBackgroundColor(Color.WHITE);
        this.addView(tvc);
    }

    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if(this.getChildCount() > 0) {
            View child = this.getChildAt(0);
            View p = (View)this.getParent();
            child.layout(20, 20, p.getWidth() - 40, p.getHeight() - 40);
        }
    }

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    class TestViewChild extends ViewGroup {
        public TestViewChild(Context context) {
            super(context);
            this.setOnClickListener(new TestClickListener());
        }

        protected void onLayout(boolean changed, int l, int t, int r, int b) {
            if(this.getChildCount() > 0) {
                View child = this.getChildAt(0);
                child.layout(10, 10, 40, 40);
            }
        }

        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
        }
    }

    class TestView extends View {
        private Paint p;
        public TestView(Context context) {
            super(context);
            p = new Paint();
            p.setColor(Color.RED);
            p.setStyle(Style.FILL);
        }

        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            int centerX = this.getWidth() / 2;
            int centerY = this.getHeight() / 2;
            canvas.drawCircle(centerX, centerY, 5, this.p);
        }

        protected void onLayout(boolean changed, int l, int t, int r, int b) {
            super.onLayout(changed, l, t, r, b);
        }

        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
        }
    }

    class TestClickListener implements OnClickListener {
        public void onClick(View v) {
            ViewGroup vg = (ViewGroup)v;
            TestView tv = new TestView(vg.getContext());
            tv.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            tv.setBackgroundColor(Color.GREEN);
            vg.addView(tv);
            tv.bringToFront();
        }
    }
}

如果我在 ViewGroup2 构造函数中创建视图,则会显示视图,但从 onclick 事件创建视图时不会发生。

提前致谢,祝您假期愉快。

【问题讨论】:

    标签: android android-layout android-widget


    【解决方案1】:

    从源代码中浮现出两件事:

    1/ 您没有为任何视图提供 LayoutParams - 您应该在将视图添加为子视图时使用它们。

    2/ 您的 onMeasure 代码在所有三种情况下都不正确。您正在使用 super 进行测量,然后从规范中获取大小(可能为 0)并覆盖 super 测量的大小。将它们全部删除,让 super 处理它们。

    【讨论】:

    • 感谢您的回答。我已将代码添加到我的帖子中,希望您能帮助我。我已经使用了颜色,并且两个视图组都正确显示了。
    • 再次感谢。实现了 OnMeasure 方法以在其中放置日志以检查维度是否不为 0。我删除了 onMeasure 方法并为每个视图提供了 LayoutParams,但行为是相同的。您可以看到上面的更改(我已按照您的建议更改了代码)
    • WRAP_CONTENT for TestView 的大小意味着 View 将报告其大小。因此,请尝试为此覆盖 onMeasure 并在函数中添加一行 - setMeasuredDimension(100,100)。