0

私はRelativeLayoutクラスを拡張し、必要に応じてすべてのコンストラクターを実装しました。また、通常のRelativeLayoutの場合と同じように、xmlレイアウトに子を追加しました。問題は、カスタムレイアウトで子にアクセスできないように見えることです。奇妙なことに、(グラフィカルレイアウトで)子ビューを選択でき、カスタムレイアウトは「親」として記述されますが、その逆を行おうとするとサイコロは表示されません。

クラスに実際に子供がいることを「伝える」にはどうすればよいですか?以下のコード!

ありがとう!

private void init(Context context) {
    System.out.println(this.getChildCount()); // Always returns 0
}

public CustRLayout(Context context) {
    super(context);
    init(context);
}

public CustRLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

public CustRLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(context);
}

そしてXMLファイル:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <app.hobi.CustRLayout android:id="@+id/cust"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
        <ImageView 
            android:id="@+id/childView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/img"
            android:src="@drawable/an_image" />
    </app.hobi.CustRLayout>

    <app.hobi.SomeOtherView android:id="@+id/other"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

</RelativeLayout>
4

1 に答える 1

2

子は後で(コンストラクターを呼び出した後)を介して追加されaddChild()ます。したがって、遅い状態でchildCountをクエリします。例:onMeasure()で、またはViewTreeObserverを使用します。

于 2012-04-23T13:02:27.827 に答える