android - layout_weight 不适用于 Drawerlayout 内的 LinearLayout

标签 android android-layout android-linearlayout drawerlayout

我在使用 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 中的预览显示了这一点:

/image/UmbxX.jpg

但是,在我的 Nexus 5 上运行时,显示以下内容:

/image/SIzwk.jpg

正如您所看到的,layout_weight =“1”部分根本没有显示。然而,当我使用内部 LinearLayout 创建一个空白项目时,布局有效(蓝色布局占据了大部分屏幕,红色布局位于底部,就像预览一样)。

任何想法都会非常感激,因为我现在完全被难住了。提前致谢。

最佳答案

看来我使用的是较旧的 Android SDK 平台。当我进入 SDK 管理器并安装 API 19 后,它就可以工作了(使用一个新项目)。

也许这是旧实现中的一个错误?

关于android - layout_weight 不适用于 Drawerlayout 内的 LinearLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24978141/

相关文章:

android - 应用内购买 : "native" and 3rd party allowed simultaneously?

android - 在 Ionic 4 应用程序中进行的 API 调用不适用于 Android 设备

android - 切换到 HEREMap premium SDK 后性能下降

android - 按钮未显示在模拟器中

Android:自定义菜单创建

android - Android 中线性布局中按钮的右对齐,按钮并排

android - Google Play 游戏服务和 libGDX "SignInActivity must be started with startActivityForResult"

android - 图像在另一台设备上模糊

java - 在循环中使用 Java LayoutInflater 添加 View 要么不设置宽度,要么只添加循环中的第一项

android - 如何通过单击按钮添加和删除线性布局