onAttachedToWindow()未被调用

时间:2014-03-23 16:07:18

标签: android

我正在为类FlyOutContainer创建一个像这样的对象

this.root = (FlyOutContainer) this.getLayoutInflater().inflate(R.layout.activity_sample, null);

在FlyOutContainer类中,我有方法onAttachedToWindow()

protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        Log.i("onAttachedToWindow", " occured");
        this.menu = this.getChildAt(0);

        this.content = this.getChildAt(1);

        this.menu.setVisibility(View.GONE);
    }

希望在为this.root创建对象时调用此方法,但是永远不会调用onAttachedWindow()方法。为什么??

UPDATE1

<com.bibliotheque.android.view.viewgroup.FlyOutContainer xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#444488"
        android:orientation="vertical" >

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="toggleMenu"
            android:text="@string/home_btn_label" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="toggleMenu"
            android:text="@string/search_btn_label" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="toggleMenu"
            android:text="@string/mybooks_btn_label" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="toggleMenu"
            android:text="@string/contact_btn_label" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="toggleMenu"
            android:text="@string/about_btn_label" />

    </LinearLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:background="#888888"
        android:orientation="vertical" >

        <LinearLayout

            android:id="@+id/home"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:background="#888888"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/hello_world"
                android:textAppearance="?android:attr/textAppearanceLarge" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/search_layout"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:background="#888888"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/searchInput"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="top"
                android:inputType="text" />

            <ListView
                android:id="@+id/bookList"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />           

        </LinearLayout>

    </LinearLayout>

我正在使用FlyOutContainer来实现幻灯片菜单
    

1 个答案:

答案 0 :(得分:0)

您是否真的将视图添加到布局中?

您需要在要包含FlyOutContainer的父级上调用addView(root),或在LayoutInflater上使用3参数方法:

getLayoutInflater().inflate(R.layout.activity_sample, container, true)

其中container再次是包含新视图的ViewGroup