Android 布局\重量不适用于抽屉内线性布局

Android 布局\重量不适用于抽屉内线性布局,android,android-layout,android-linearlayout,drawerlayout,Android,Android Layout,Android Linearlayout,Drawerlayout,在使用抽屉布局时,我正在努力使用LinearLayout。这是使用Android Studio模板进行抽屉布局: <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"

在使用抽屉布局时,我正在努力使用LinearLayout。这是使用Android Studio模板进行抽屉布局:

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

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

如您所见,布局_weight=“1”零件根本不显示。然而,当我创建一个带有内线布局的空白项目时,布局是有效的(蓝色布局占据了大部分屏幕,底部是红色布局,就像预览一样)

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

  • 在抽屉布局中,您应该有一个框架布局,其中包含屏幕的主要内容,并且在大多数情况下,您还应该有一个列表视图来保存导航抽屉的内容
  • 它应该是这样的:

    <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">
        <!-- The main content view -->
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <!-- The navigation drawer -->
        <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="#111"/>
    </android.support.v4.widget.DrawerLayout>
    

    如果它解决了您的问题,请将此标记为答案。多谢各位

    更新

    好的,首先删除“xmlns:android=”http://schemas.android.com/apk/res/android“从你的直线布局。这应该只在主根xml中出现一次,这会导致抽屉布局

    接下来,确定如何使用体重。声明一个android:weightSum属性。把重量总和想象成一个馅饼。在这种情况下,我们把它也设为5,所以我们有5块馅饼。使用android:layout\u-weight属性在该布局内的任何内容。第一个设置为4,占屏幕的四分之五。最后一个是底部屏幕的1/5

    确保此LinearLayout仍包装在FrameLayout中。您的线性布局应如下所示:

    <LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="5">
        <LinearLayout
            android:background="#00f"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="4"/>
        <LinearLayout
            android:background="#f00"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0" />
        </LinearLayout>
    </LinearLayout>
    

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


    可能是旧版本实现中的错误?

    这肯定是Android的问题(或者至少是
    appcompat/design
    library的问题)。我在抽屉布局中测试了不同的布局,得出的结论是问题出在“
    content\u layout
    ”文件——抽屉下方显示的文件


    只有使用干净、简单的框架布局才有效。没有协调布局,没有碎片,没有自定义布局,也没有第三方布局,只有一个很好的旧框架布局,宽度和高度为“
    匹配\u parent
    ”。

    对于抽屉布局,我认为您最好使用框架布局作为内容容器,并将线性布局放在框架布局中。我试着添加frameLayout来环绕LinearLayout。同样的事情发生了。天哪,这正是我想问的!对我来说,一个带有权重的简单布局是可行的,但一个更复杂的布局却不行。我还没有分离出这个拼图的碎片,但是下面的解决方案都不起作用。不幸的是,使用FrameLayout和我的LinearLayout在里面不起作用。在我的Nexus 5上也发生了同样的行为。@Farplaner看到我更新的答案,我相信我解决了你的问题。您错误地使用了权重属性。您应该在父布局中使用android:weightSum,在嵌套子布局中使用android:layout\u weight。如果答案正确,请将其标记为:)谢谢您的更新答案。然而,它仍然不适合我。但一定是别的什么原因引起了我的问题。它现在只适用于我的新项目和新API。我无法转到现有项目并更改API/sync gradle。将所有内容包装在
    框架布局中对我来说并不能解决此问题。使用
    weightSum
    也无法修复它。请确保测试它是否在早期API上运行。我仍然建议您看看我告诉您的关于使用android:weightSum的内容,因为如果设计不当,您的线性布局在将来可能会出现问题。