【问题标题】:Toolbar overlapping layout. I have tried many solutions none work工具栏重叠布局。我尝试了很多解决方案都没有工作
【发布时间】:2019-09-28 22:09:22
【问题描述】:

我的工具栏超出了我的布局,我想修复它。我试过这些 How to make Toolbar not overlap other content in Android?

Android AppBarLayout overlaps listview

Where should 'app:layout_behavior' be set?

它仍然无法正常工作,您可以在这里看到我的代码:

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        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"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:showIn="@layout/app_bar_main"
        tools:context=".MainActivity">
    <android.support.v4.view.ViewPager
            android:layout_width="395dp"
            android:layout_height="659dp" app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent" android:id="@+id/viewPager"/>
</android.support.constraint.ConstraintLayout>

app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
        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"
        tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay"/>

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</android.support.design.widget.CoordinatorLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
        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:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">

    <include
            layout="@layout/app_bar_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

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

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

似乎没有任何效果(可能是因为视图寻呼机?)

【问题讨论】:

    标签: android xml


    【解决方案1】:

    你可以试试这个,而不是包含多个布局,只需在你的 activity_main.xml 中使用一个

    **enter code here**
    <?xml version="1.0" encoding="utf-8"?>
      <android.support.v4.widget.DrawerLayout
        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:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
    
    <android.support.design.widget.AppBarLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:theme="@style/AppTheme.AppBarOverlay">
    
        <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay"/>
    
    </android.support.design.widget.AppBarLayout>
    
    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        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"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header_main"
            app:menu="@menu/activity_main_drawer"/>
    
    </android.support.v4.widget.DrawerLayout> 
    

    【讨论】:

    • 谢谢,复制并粘贴您的代码,它可以工作。我猜在项目开始时选择抽屉活动是不正确的。
    • 不,与选择无关,这是简单的布局规则。即使您尝试仅通过在 app_bar_main.xml 中使用 linearLayout 更改 CoordinatorLayout 也可以,我只是将其简化为一个 xml。
    • 好的,谢谢您的解释。我要阅读布局规则。
    【解决方案2】:

    我对 xml 进行了一些更改,但您可以根据需要转换为 Constraintlayout。这是我设计的简单 UI 来帮助你理解

    <androidx.drawerlayout.widget.DrawerLayout
        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:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".tasks.TasksActivity"
        tools:openDrawer="start">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <com.google.android.material.appbar.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="?attr/colorPrimary"
                    android:minHeight="?attr/actionBarSize"
                    android:theme="@style/Toolbar"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
            </com.google.android.material.appbar.AppBarLayout>
    
            <androidx.coordinatorlayout.widget.CoordinatorLayout
                android:id="@+id/coordinatorLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
                 <android.support.v4.view.ViewPager
                android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:id="@+id/viewpager  />
    
                <com.google.android.material.floatingactionbutton.FloatingActionButton
                    android:id="@+id/fab_add_task"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="@dimen/fab_margin"
                    android:src="@drawable/ic_add"
                    app:layout_anchor="@id/contentFrame"
                    app:layout_anchorGravity="bottom|right|end"
                    app:fabSize="normal" />
            </androidx.coordinatorlayout.widget.CoordinatorLayout>
    
        </LinearLayout>
    
        <!-- Navigation Drawer -->
        <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header"
            app:menu="@menu/drawer_actions" />
    
    </androidx.drawerlayout.widget.DrawerLayout>
    

    【讨论】:

    • 那么我应该改变什么来解决我的问题?
    • @Dave 在 app_bar_main.xml 中使用坐标有点错误,它会在另一个视图上膨胀一个视图,因此必须有 LinearLayout 而不是 CoordinateLayout,并且您可以使用视图寻呼机的坐标顶部来添加晶圆厂或底部工作表。就这么简单
    猜你喜欢
    • 1970-01-01
    • 2022-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    • 1970-01-01
    • 2021-01-14
    相关资源
    最近更新 更多