正在尝试 here 中提到的抽屉布局示例
尝试之后,我在主布局中添加了一些元素,例如按钮。这就是我所做的:
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="openTestButtonActivity"
android:text="@string/testing" />
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
.
.
.
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
现在,抽屉工作正常,但我无法激活我的按钮。我做错的布局顺序有什么问题吗?
最佳答案
是的,布局顺序错误。在 relativeLayout 中, View 的排序就像在代码中添加的一样。因此,如果您先添加按钮,然后再添加 drawerLayout,drawerLayout 会覆盖按钮。所以你应该先添加 DrawerLayout,然后再添加 Button。
关于具有主要 Activity 的 Android 抽屉布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17628219/