框架布局中的嵌套线性布局导致问题

时间:2015-05-26 09:51:59

标签: android android-layout layout

我已经尝试了几种不同版本的布局,但每种版本都有一些不起作用的症状。

我只是想要一个有效的幻灯片菜单。

选项一(框架布局中的嵌套线性布局):

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <!-- Framelayout to display Fragments -->
    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
    <!-- Listview to display slider menu -->
    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector"
        android:background="@color/list_background"
        />
    <LinearLayout
        android:visibility="gone"
        android:background="@drawable/background_parchment"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/challengerWandIdEt"
        android:enabled="true"
        android:hint="WandId of challenger"
        android:clickable="false" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Accept battle from"
        android:id="@+id/challengeBtn"
        android:enabled="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Log out"
        android:id="@+id/logOutBtn"
        android:clickable="true"
        android:enabled="true" />

    <model.TypeWriter
            android:layout_gravity="center_horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/battleMode"
            android:text=""
            android:textColor="@color/main_menu_text_color_selector"
            android:enabled="true"
            android:clickable="true"
            android:singleLine="true"
            android:lines="1"/>
    <model.TypeWriter
            android:layout_gravity="center_horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/spells"
            android:text=""
            android:textColor="@color/main_menu_text_color_selector"
            android:enabled="true"
            android:clickable="true" />
    <model.TypeWriter
            android:layout_gravity="center_horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/hightScore"
            android:text=""
            android:textColor="@color/main_menu_text_color_selector"
            android:enabled="true"
            android:clickable="true" />
    <model.TypeWriter
            android:layout_gravity="center_horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/endAllBattles"
            android:text=""
            android:textColor="@color/main_menu_text_color_selector"
            android:enabled="true"
            android:clickable="true"
            android:focusable="true"
        />
    </LinearLayout>
    </FrameLayout>
</android.support.v4.widget.DrawerLayout>

导致:java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams

选项2(我将LinearLayout的可见性设置为gone并在FrameLayout开始之前结束LinearLayout时正常工作,问题是,我想看看LinearLayout的内容:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <!-- Framelayout to display Fragments -->
    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        />
    <!-- Listview to display slider menu -->
    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector"
        android:background="@color/list_background"
        />
    <LinearLayout
        android:visibility="gone"
        android:background="@drawable/background_parchment"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/challengerWandIdEt"
        android:enabled="true"
        android:hint="WandId of challenger"
        android:clickable="false" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Accept battle from"
        android:id="@+id/challengeBtn"
        android:enabled="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Log out"
        android:id="@+id/logOutBtn"
        android:clickable="true"
        android:enabled="true" />

    <model.TypeWriter
            android:layout_gravity="center_horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/battleMode"
            android:text=""
            android:textColor="@color/main_menu_text_color_selector"
            android:enabled="true"
            android:clickable="true"
            android:singleLine="true"
            android:lines="1"/>
    <model.TypeWriter
            android:layout_gravity="center_horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/spells"
            android:text=""
            android:textColor="@color/main_menu_text_color_selector"
            android:enabled="true"
            android:clickable="true" />
    <model.TypeWriter
            android:layout_gravity="center_horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/hightScore"
            android:text=""
            android:textColor="@color/main_menu_text_color_selector"
            android:enabled="true"
            android:clickable="true" />
    <model.TypeWriter
            android:layout_gravity="center_horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/endAllBattles"
            android:text=""
            android:textColor="@color/main_menu_text_color_selector"
            android:enabled="true"
            android:clickable="true"
            android:focusable="true"
        />
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

选项3: 与前一个相同,但LinearLayout visiblity未设置为已消失。 然后一切似乎都有效,但点击菜单元素后,不会触发OnItemClick事件。

我搜索过此问题,并且每个人都建议将focusableclickable更改为false,但这没有用。或许我做错了什么。 (我已将clickablefocusable添加到主要活动的每个元素中

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您需要在主框架布局中添加视图内容,在滑动菜单旁边添加:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- Your content -->

    </FrameLayout>


    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

对于你的例子,你将有

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:visibility="gone"
            android:background="@drawable/background_parchment"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/challengerWandIdEt"
                android:enabled="true"
                android:hint="WandId of challenger"
                android:clickable="false" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Accept battle from"
                android:id="@+id/challengeBtn"
                android:enabled="true" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Log out"
                android:id="@+id/logOutBtn"
                android:clickable="true"
                android:enabled="true" />

            <model.TypeWriter
                    android:layout_gravity="center_horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/battleMode"
                    android:text=""
                    android:textColor="@color/main_menu_text_color_selector"
                    android:enabled="true"
                    android:clickable="true"
                    android:singleLine="true"
                    android:lines="1"/>

            <model.TypeWriter               
                    android:layout_gravity="center_horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/spells"
                    android:text=""
                    android:textColor="@color/main_menu_text_color_selector"
                    android:enabled="true"
                    android:clickable="true" />

            <model.TypeWriter
                    android:layout_gravity="center_horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/hightScore"
                    android:text=""
                    android:textColor="@color/main_menu_text_color_selector"
                    android:enabled="true"
                    android:clickable="true" />
            <model.TypeWriter
                    android:layout_gravity="center_horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/endAllBattles"
                    android:text=""
                    android:textColor="@color/main_menu_text_color_selector"
                    android:enabled="true"
                    android:clickable="true"
                    android:focusable="true"
                />
            </LinearLayout>
    </FrameLayout>


    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>