我在使用 DrawerLayout 时遇到了 LinearLayout 的问题。这是使用 Android Studio 模板进行 DrawerLayout:
<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=".MyActivity">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:background="#00f"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:background="#f00"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0" />
</LinearLayout>
</LinearLayout>
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
If you're not building against API 17 or higher, use
android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
the container. -->
<fragment android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.test.testdrawerlayout.NavigationDrawerFragment"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
Android Studio 中的预览显示了这一点:
但是,在我的 Nexus 5 上运行时,显示以下内容:
正如您所看到的,layout_weight =“1”部分根本没有显示。然而,当我使用内部 LinearLayout 创建一个空白项目时,布局有效(蓝色布局占据了大部分屏幕,红色布局位于底部,就像预览一样)。
任何想法都会非常感激,因为我现在完全被难住了。提前致谢。
最佳答案
看来我使用的是较旧的 Android SDK 平台。当我进入 SDK 管理器并安装 API 19 后,它就可以工作了(使用一个新项目)。
也许这是旧实现中的一个错误?
关于android - layout_weight 不适用于 Drawerlayout 内的 LinearLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24978141/