Android 工具栏与线性布局重叠
我有一个线性布局和两个内部线性布局。如果我在这个布局文件中添加工具栏,它总是与整个布局重叠。因此,只有工具栏可见。在其他布局文件中,它工作没有任何问题Android 工具栏与线性布局重叠,android,android-layout,android-toolbar,Android,Android Layout,Android Toolbar,我有一个线性布局和两个内部线性布局。如果我在这个布局文件中添加工具栏,它总是与整个布局重叠。因此,只有工具栏可见。在其他布局文件中,它工作没有任何问题 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.andro
<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>
这里有什么问题?第一个
线性布局的方向错误。它应该设置为垂直
,而不是水平
,这将在宽度屏幕右侧的工具栏
后绘制其他子项(作为内部线性布局
s)。改为:
<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">
在其他活动中重复使用此工具栏布局:
<?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 ...>
<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的预览版中)。@DenoAgüero它似乎按照预期工作(请参见我的编辑)。也许你必须与2个线性布局的内部子项进行核对。您应该避免嵌套权重以获得更好的性能。谢谢。但是现在我的布局是相互之间的,而不是并排的。@DenoAgüero那么你想要工具栏在顶部,然后两个内部线性布局并排?那么,用相对长度替换外部线性布局可能会更容易。@Denagüero哦,对不起,我没有注意到你想要并排。编辑;)您可以尝试添加android:fitsystemwindows=“true”。提到
<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>