【问题标题】:NavigationDrawer blocks other button on the activityNavigationDrawer 阻止活动上的其他按钮
【发布时间】:2015-01-06 00:26:22
【问题描述】:

我正在处理我的应用程序的 UI,但在实现 Navigation Drawer 时遇到了问题。正如你所看到的here,我有一些按钮来管理我的媒体播放器,当我实现导航抽屉时,这些按钮没有响应我的点击(没有导航抽屉也可以正常工作)。

我认为问题出在我的 XML 文件中,因为当我删除 Java 中的实现时没有任何变化。

这是我的 XML 表单:

ma​​in_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MyActivity">

    <fragment
        android:id="@+id/fragments"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.example.ilan.myapplication.fragments.FragmentHome" >
    </fragment>


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Buffering"
        android:id="@+id/tV_Buffering"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true" />

    <include
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_weight="1"
        layout="@layout/player_controls"
        android:layout_alignParentBottom="true"/>

    <include
        layout="@layout/navigation_drawer_main_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

请注意,我把导航抽屉放在最后是为了让它覆盖所有活动。

navigation_drawer_main_layout.xml

<?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">

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

    <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="#FFFFFF"/>
</android.support.v4.widget.DrawerLayout>

player_controls.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blue">

        <Button
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:id="@+id/button_play_pause"
            android:layout_toStartOf="@+id/tV_Buffering"
            android:background="@drawable/bouton_play"
            android:layout_centerInParent="true"
           />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Stop"
            android:id="@+id/button_stop"
            android:layout_toRightOf="@id/button_play_pause"
            android:layout_centerInParent="true"
            android:layout_marginLeft="20dp"/>
</RelativeLayout>

好吧,如果有人知道这是从哪里来的,那就太好了,谢谢!

【问题讨论】:

  • 不将 player_controls 包含在 main_activity 中,而是将它们作为片段的一部分实现
  • @VyprNoch 我不明白这会有什么不同?我的意思是在我的问题中,这不会影响任何事情,不是吗?因为否则这意味着我们在使用导航抽屉时只能使用片段,在我的情况下,播放器将始终是活动的一部分。
  • 可能是因为“navigation_drawer_main_layout”在“player_controls”之上,所以它会获得点击/触摸事件。尝试将“xx 小于你的一半屏幕,不隐藏“播放按钮”)
  • Arf,没有效果,我在
  • 您的问题解决了吗?我有同样的问题,但无法解决。

标签: android xml user-interface button navigation-drawer


【解决方案1】:

要使用 DrawerLayout,请将您的主要内容视图定位为第一个子视图,其宽度和高度为 match_parent 且没有 layout_gravity。在主内容视图之后添加抽屉作为子视图,并适当地设置 layout_gravity。抽屉通常使用 match_parent 作为高度和固定宽度。查看谷歌关于 DrawerLayout 的文档:https://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html

您的主要内容应该放在 DrawerLayout 中,而不是在您的 RelativeLayout 中包含 DrawerLayout。

对于您的情况,您可以尝试以这种方式实现它:

ma​​in_activity.xml

<?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">

    <RelativeLayout
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MyActivity">

        <fragment
            android:id="@+id/fragments"
            class="com.example.ilan.myapplication.fragments.FragmentHome"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </fragment>


        <TextView
            android:id="@+id/tV_Buffering"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true"
            android:text="Buffering"
            android:textAppearance="?android:attr/textAppearanceSmall"/>

        <include
            layout="@layout/player_controls"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_alignParentBottom="true"
            android:layout_weight="1"/>

    </RelativeLayout>

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多