0

ビューとビューグループの動作を理解しようとするテストを開発しています。非常に単純なカスタム ビューグループ (以下ビューグループ 1) を作成し、すべての重要なメソッド (onLayout、onMeasure、onSizeChanged) を正しく実装しました。次に、単純なビューを作成し、そのビューを onclick イベントから viewgroup1 に追加します。ビューが正しく表示され、そのメソッドが呼び出されます (onSizeChanged、onLayout、onDraw)。

このテストが成功した後、別のカスタム ビュー グループ (以降、ビュー グループ 2) を作成し、今回はビュー グループ 2 をビュー グループ 1 に追加しました。次に、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 イベントから作成されたときには表示されません。

よろしくお願いします。良い休日をお過ごしください。

4

1 に答える 1

0

ソース コードから 2 つのことが思い浮かびます。

1/ どのビューにも LayoutParams を提供していません - ビューを子として追加するときにそれらを使用する必要があります。

2/ onMeasure コードは 3 つのケースすべてで正しくありません。スーパーを使用して測定を行い、スペックからサイズを取得し(0の場合があります)、スーパーが測定したものをオーバーライドします。それらをすべて削除して、スーパーに処理させます。

于 2012-12-30T10:40:54.837 に答える