android - 工具栏与 LinearLayout 重叠

标签 android android-layout android-toolbar

我有一个带有 2 个内部 LinearLayout 的 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>

这里有什么问题?

最佳答案

第一个 LinearLayout 方向错误。它应该设置为 vertical,而不是 horizo​​ntal,后者让其他子项(作为内部 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>

我在其他 Activity 中重复使用此工具栏布局的地方:

<?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"/>

输出:

Screenshot of Toolbar above two LinearLayouts

我的测试包含 above/below inner children,但为了满足您的要求,只需为 child 添加一个父容器,很容易做到:

<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 - 工具栏与 LinearLayout 重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41289419/

相关文章:

来自数据库的 Android SimpleCursorAdapter ID

android - 回收站 View 项目不滚动?

android - Android Studio 2.2.1 中使用约束布局的布局编辑器问题

android - ViewPager 滚动布局行为不适用于工具栏

android - 如何在 Android 中更改工具栏标题的颜色?

android - google-api-java-client NetHttpTransport 导致 NoClassDefFoundError

android - 如何将图像json数据放入listview中

java - 将 ArrayList 转换为数组会导致崩溃

java - 如何在“操作”选项卡中回调按钮

android - 折叠工具栏向上滚动时隐藏,向下滚动时显示