工具栏与线性布局重叠

3

我有一个LinearLayout,其中包含2个内部LinearLayout。如果我在这个布局文件中添加工具栏,它总是会重叠整个布局。因此,只有工具栏可见。在其他布局文件中,它没有任何问题。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="horizontal"
          tools:context="de.dk.mafi.ActMain">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="?attr/colorPrimary"
    android:fitsSystemWindows="true"
    android:minHeight="?attr/actionBarSize"
    android:padding="2dp"
    app:titleMarginStart="20dp"
    app:titleTextAppearance="@style/MyMaterialTheme.Base.TitleTextStyle"
    app:titleTextColor="@color/textColorPrimary">

    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="TEST"
        android:textColor="@android:color/white"
        android:textStyle="bold|italic"/>

</android.support.v7.widget.Toolbar>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_margin="20dp"
    android:layout_weight="1"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@drawable/border"
        android:padding="10dp"
        android:text="@string/welcome"/>

    <Button android:id="@+id/button2" android:layout_width="match_parent"
            android:layout_height="wrap_content" android:text="Favoriten"/>


</LinearLayout>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_margin="20dp"
    android:layout_weight="1"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:src="@drawable/training"/>

    <Button android:id="@+id/button" android:layout_width="match_parent"
            android:layout_height="wrap_content" android:text="Hauptmenü"/>

</LinearLayout>

这里有什么问题?


你可以尝试添加android:fitsSystemWindows="true"。请参考https://medium.com/google-developers/why-would-i-want-to-fitssystemwindows-4e26d9ce1eec#.q6n66hugs。 - Angel Koh
1个回答

2

第一个LinearLayout的方向设置错误。应该使用vertical而不是horizontal,这会导致其他子元素(如内部的LinearLayout)在屏幕宽度右侧的Toolbar之后绘制。请更改为:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      ...
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      android:fitsSystemWindows="true">

然后,从 Toolbar 中删除 android:fitsSystemWindows="true"

编辑:

我刚刚做了这个,它按预期工作:

<LinearLayout 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:orientation="vertical"
    android:fitsSystemWindows="true"
    tools:context="...">

    <include layout="@layout/include_toolbar" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/blue"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/red"/>
</LinearLayout>

当我在其他活动中重用此工具栏布局时:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"/>

输出:

两个LinearLayout上方的工具栏截图

我的测试包含上下内部子项,但为了满足您的要求,只需添加一个父容器来容纳这些子项,如下所示:

<LinearLayout ...>

   <include layout="@layout/include_toolbar" />

   <!-- use a parent container with horizontal orientation -->
   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="horizontal">

       <LinearLayout
           android:layout_width="0dp"
           android:layout_height="match_parent"
           android:layout_weight="1" .../>

       <LinearLayout
           android:layout_width="0dp"
           android:layout_height="match_parent"
           android:layout_weight="1" .../>
    </LinearLayout>
</LinearLayout>

我已经尝试过了,但在Android Studio的预览中显示的结果是相同的。 - Deno Agüero
@DenoAgüero 看起来它按预期工作(请查看我的编辑)。也许您需要检查两个线性布局的内部子元素。为了获得更好的性能,应避免嵌套权重。 - Blo
谢谢。但是现在我的布局是相互之间而不是并排的。 - Deno Agüero
@DenoAgüero,所以您想要工具栏在顶部,然后两个内部LinearLayout并排放置?那么,用RelativeLayout替换外部LinearLayout可能会更容易些。 - Ridcully
1
@DenoAgüero 噢,抱歉,我没有注意到你想要并排。已经编辑好了 ;) - Blo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接