android studio中的布局重叠

时间:2018-12-04 16:04:58

标签: java android xml android-studio

所以我试图制作一个应用程序,但是当我尝试使用多个布局时。但是它们彼此重叠。

enter image description here

enter image description here

这些是设计的布局文件

这是nav_drawer

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"
android:background="#429B50"
tools:openDrawer="start">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />


   </android.support.v4.widget.DrawerLayout>
   </RelativeLayout>

这是nav_home

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">


<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

   </RelativeLayout>

这是活动首页

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout   xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">


<include layout="@layout/nav_drawer"/>
<include layout="@layout/nav_home"/>

</RelativeLayout>

我知道它看起来与多个布局文件混在一起,但这是因为我正在尝试不同的方法来解决问题。非常感谢您的帮助

2 个答案:

答案 0 :(得分:0)

在活动家(即RelativeLayout)中,包括以下内容:

<include layout="@layout/nav_drawer"/>
<include layout="@layout/nav_home"/>

您必须使用正确的属性来正确放置这些布局。
如果您希望按钮位于活动的底部,请执行以下操作:

<include layout="@layout/nav_home"
 android:layout_alignParentBottom="true"/> 

您可能还需要设置其他属性。
同样在nav_home.xml中,我认为您应该至少为高度设置android:layout_height="wrap_content",也许也为宽度设置ConstraintLayout,并删除仅在<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:text="Button" /> </RelativeLayout> 内部适用的所有这些属性:


    ...
    chromeOptions: {
          prefs: {
            AutoSelectCertificateForUrls: [
              '{"pattern":"[*.]my.website.here","filter":{"ISSUER":{"CN":"My Issuer Here"}}}'
            ]
          }
    ...

答案 1 :(得分:0)

如果您希望按钮位于右下角,则可以使用以下两种方法之一:

  1. 您可以使用约束布局并将这两个约束添加到Button app:layout_constraintBottom_toBottomOf =“ parent” app:layout_constraintEnd_toEndOf =“ parent”

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />
    

  2. 修改相对布局并添加此

    android:layout_alignParentRight =“ true”   android:layout_alignParentBottom =“ true”

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="Button"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>

希望可以解决您的问题...:)

相关问题