Android 协调布局中的重叠视图
代码如下。加载片段时,它会覆盖工具栏和浮动操作栏。我该怎么办?我希望工具栏显示。在协调布局中似乎存在一些重叠。我检查了不同的问题,像这样。似乎没有人回答我的问题Android 协调布局中的重叠视图,android,android-layout,user-interface,material-design,Android,Android Layout,User Interface,Material Design,代码如下。加载片段时,它会覆盖工具栏和浮动操作栏。我该怎么办?我希望工具栏显示。在协调布局中似乎存在一些重叠。我检查了不同的问题,像这样。似乎没有人回答我的问题 <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout android:layout_width="fill_parent" android:layout_height
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="@dimen/activity_horizontal_margin"
android:orientation="vertical"
tools:context="com.example.sammybobo.moglis.MoGLISMaps"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<android.support.v7.widget.Toolbar
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="?attr/actionBarSize"
android:id="@+id/toolbar"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.design.widget.FloatingActionButton
android:id = "@+id/fab"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src = "@android:drawable/ic_input_add"
android:layout_gravity = "bottom|end"
app:fabSize = "mini">
</android.support.design.widget.FloatingActionButton>
<fragment
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/main_map"
android:name="com.google.android.gms.maps.SupportMapFragment"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:context = "com.example.sammybobo.moglis.MoGLISMaps"
xmlns:tools="http://schemas.android.com/tools"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:layout_gravity = "bottom"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="My location"
android:onClick="observe"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text = "Friends"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text = "settings"/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
这是因为在协调器布局中,您无法设置一个视图与另一个视图的关系。您只能将它们与父级关联。试试下面的布局
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical"
android:padding="@dimen/activity_horizontal_margin"
tools:context="com.example.sammybobo.moglis.MoGLISMaps">
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="fill_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"/>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="@+id/toolbar"
tools:context="com.example.sammybobo.moglis.MoGLISMaps"
/>
</RelativeLayout>
<LinearLayout
android:background="#fff"
android:layout_gravity="bottom"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
>
<Button
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="wrap_content"
android:onClick="observe"
android:text="My location"/>
<Button
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="wrap_content"
android:text="Friends"/>
<Button
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="wrap_content"
android:text="settings"/>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
app:fabSize="mini"
android:id="@+id/fab"
android:layout_gravity="bottom|end"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@android:drawable/ic_input_add">
</android.support.design.widget.FloatingActionButton>
</android.support.design.widget.CoordinatorLayout>
如果您希望底部线性布局也不与片段重叠,则将其放入相对布局中,并确保片段位于线性布局上方。您也可以将所有视图包装在
线性布局中,以防止任何重叠。非常感谢。它可以工作,但如何防止relativelayout与工具栏重叠工具栏位于相对布局内。为什么会重叠呢?对不起,相对论你和晶圆厂重叠了。我已经编辑了我的答案。将晶圆厂布局移到底部。底部的布局将最后绘制,因此将在已绘制的相对布局上绘制。你想把晶圆厂放在相对布局的正上方。很高兴我能帮忙。这实际上是一个非常好的主意。我会随时通知你的。