【问题标题】:Menu from Activity is not overflowing Fragment来自活动的菜单没有溢出片段
【发布时间】:2017-02-04 13:12:13
【问题描述】:

问题:我创建了包含 Drawer 和 FrameLayout 的 Activity,它们将被片段膨胀。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">


    <FrameLayout
        android:id="@+id/main_fragment_panel"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <!-- The navigation drawer -->
    <RelativeLayout
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:id="@id/drawerPanel"
        android:layout_gravity="start"
        android:background="@color/backgroundColor">

        <-- CONTENT->>
    </RelativeLayout>





    </android.support.v4.widget.DrawerLayout>

在将片段更改/膨胀到 FrameLayout main_fragment_panel 后,抽屉应该打开(日志中的所有内容看起来都很好),但看起来片段从活动中溢出了所有内容。如何更改布局的“级别”或“优先级”?
更新:我已经更新了上面的代码 - 现在可以正常显示,但没有处理来自 (那里有一些带有监听器的按钮)

【问题讨论】:

  • 将 FrameLayout 放入 DrawerLayout 标签内
  • 谢谢@tahsinRupam。请单独发布,以便我可以标记为答案;)顺便说一句。现在菜单中的项目不可点击:(
  • 我已经编辑了我的答案。让我知道这是否有效:)

标签: android android-layout android-fragments


【解决方案1】:

您应该使用 DrawerLayout 作为父布局。这是修改后的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

     <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></FrameLayout>


    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        android:background="@color/background_material_dark"
        app:itemTextColor="@color/colorWhite"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

【讨论】:

  • 完美运行,但菜单中没有事件被处理:(点击某些菜单项后它只是关闭抽屉
  • 我现在必须离开我的办公室。当我回到家时,我会给出修改后的答案。留在我身边:p
  • 它现在可以工作了!我在我身边做了一些奇怪的事情!谢谢!
【解决方案2】:

试试类似的,

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <fragment
        android:id="@+id/right_drawer"
        android:name="com.fragments.RightFragment"//your left fragment
        android:layout_width="@dimen/drawer_width"
        android:layout_height="fill_parent"
        android:layout_gravity="left" />
</android.support.v4.widget.DrawerLayout>

【讨论】:

    【解决方案3】:

    你的抽屉布局应该是这样的;

     <?xml version="1.0" encoding="utf-8"?>
    <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 -->
    
        <LinearLayout
            android:id="@+id/llContent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <FrameLayout
                android:id="@+id/content_frame"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>
    
        <!-- The navigation drawer -->
        <!-- Menu items--->
        <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:background="@drawable/background_drawer"
        app:headerLayout="@layout/nav_header"
        app:itemIconTint="@color/colorPrimary"
        app:menu="@menu/menu_navigation" />
    
    </android.support.v4.widget.DrawerLayout>
    

    现在用于替换片段:

    FragmentTransaction.replace(R.id.content_frame, fragment, tag); 
    

    【讨论】:

    • 尽管我很喜欢 NavigationView 的想法,但在设计方面我有非常不常见的菜单,我想远离 NavigationView。我已经更新了原始问题 - 现在它可以正常打开,但没有任何事件:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-02
    • 2017-03-10
    • 1970-01-01
    相关资源
    最近更新 更多