通过片段按下按钮

Pressing button through fragment

我正在尝试创建一个应用程序,其侧面的片段作为菜单栏。

我的主要问题是,当我打开片段时,我可以看到它后面的按钮,甚至可以按下它们。

我当然不想那样。我尝试将 属性 android:clickable="true" 添加到布局中,但没有帮助。

这是我的代码:

<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"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="ibuy.ibuy.AddUpdateItem">

    .
    .
    .
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/search_image"
        android:id="@+id/btnBrows"
        android:onClick="openItemTable"
        android:layout_alignTop="@+id/imageView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
    .
    .
    .

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

        <fragment
            android:id="@+id/navigation_drawer"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:name="Menus.smallMenu"
            tools:layout="@layout/fragment_navigation_drawer"
            android:alpha="255" />

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

这是其他文件片段的listView的代码:

<ListView
    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"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#cccc"
    tools:context="Menus.smallMenu"
    android:clickable="true"/>

我做错了什么? 提前致谢!

我不知道你说的 "I can see the buttons behind it" 是什么意思。我假设您的意思是在 DrawerLayout UI 后面。

1) 在这种情况下,您可以使用 android:layout_below="@id/btnBrows" 之类的属性。据我所知,所有 UI 元素都应该定位,否则它们可能会一个接一个地显示,就像您所看到的那样。

2) 还有一个问题是 DrawerLayoutandroid:layout_widthlayout_height 都是 match_parent,它们填满了整个屏幕。它与按钮竞争屏幕 space。如果按钮在 DrawerLayout 内,那不是问题,但这可能是您的故意设计。

如果您喜欢 Android Studio,请创建一个新的 Navigation Drawer Activity。看看主要的 XML 布局。

DrawerLayout 应该是您的根视图。在其中,您有带有按钮 RelativeLayoutFragment 是您的导航抽屉。

这样,两者就分开了,导航抽屉在打开时会正确重叠。