Android Studio Android top app bar blocking fragment背景图片

inn6fuwd  于 6个月前  发布在  Android
关注(0)|答案(1)|浏览(60)

我正在开发一个Android应用程序,遇到了这个奇怪的问题。我有一个主活动,它包含一个导航抽屉和一个顶部应用程序栏,顶部应用程序栏是透明的,所以背景图像在搜索栏和导航抽屉按钮后面可见。问题是,当我使用导航抽屉导航到不同的片段时,这个片段的背景并不像主活动背景那样一直延伸到屏幕顶部。我在活动和片段上设置了一个自定义背景,你可以从导航抽屉导航到片段,但令人困惑的是,当你启动应用程序时,主屏幕片段没有特定的背景设置,工作得很好。
我也在想,有一个顶部的应用程序栏举行的主要活动是不是一个好主意,我应该有一个每个片段,但我还没有能够拉过。
理想情况下,我希望每个片段都有一个单独的顶级应用程序,但如果你能帮助我解决这个主要问题,那就已经对我有了很大的帮助。
以下是应用程序的屏幕截图和相关文件的代码片段:



以下是相关文件:
activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<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"

    android:background="@drawable/best_winter_aesthetic_phone_wallpaper_in_hd"
    tools:openDrawer="start">

    <include
        android:id="@+id/app_bar_main"
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
    />

    <com.google.android.material.navigation.NavigationView
        app:itemTextColor="@color/white"
        app:itemIconTint="@color/white"
        android:background="#80000000"
        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" />
</androidx.drawerlayout.widget.DrawerLayout>

字符串
app_bar_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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:fitsSystemWindows="true"
    >

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:background="@android:color/transparent">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
            android:background="@android:color/transparent"
            app:titleTextColor="@android:color/black">

            <!-- Search bar -->
            <androidx.appcompat.widget.SearchView
                android:id="@+id/searchView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:iconifiedByDefault="false"
                app:queryHint="Search"
                android:textAppearance="@color/white"
                android:background="@drawable/semi_transparent_rounded_searchview_background"
                app:queryBackground="@android:color/transparent" />
        </com.google.android.material.appbar.MaterialToolbar>
    </com.google.android.material.appbar.AppBarLayout>

    <include layout="@layout/content_main" />


</androidx.coordinatorlayout.widget.CoordinatorLayout>


fragment_current_location.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".presentation.CurrentLocationFragment"

    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Home"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.5" />

</androidx.constraintlayout.widget.ConstraintLayout>


fragment_account.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    tools:context=".presentation.navigation.AccountFragment"
    android:background="@drawable/generic_background"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Account"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.5" />

</androidx.constraintlayout.widget.ConstraintLayout>


content_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/app_bar_main">

    <fragment
        android:background="@android:color/transparent"
        android:id="@+id/nav_host_fragment_content_home"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>


themes.xml:

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.VibeCast" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <item name="colorPrimary">#801E1D1D</item>
        <item name="colorPrimaryVariant">#FFF0F0</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">#1F1E1E</item>
        <!-- Status bar color. -->

        <!-- This is where you should define the status bar attributes -->

        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>

    <style name="Theme.VibeCast.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="Theme.VibeCast.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="Theme.VibeCast.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>

2cmtqfgy

2cmtqfgy1#

这就是问题所在。您在AccountFragment中设置了一个自定义背景,这意味着android:background不是半透明的。如果您希望Fragment按照您的意愿加载,只需删除fragment_account. xml中的android:background="@drawable/generic_background"行。这意味着背景将是半透明的。
为什么在开始的时候会看到半透明的背景,这和你初始化NavigationDrawer的方式有关。看起来你在开始的时候并没有显示任何片段。只有当选择一个Tab时,片段才会被加载。这意味着你的Drawer的第一个状态并没有加载任何片段。为了防止这种情况,你可以复制你用来切换到你的AccountFragment的代码,并将其粘贴到NavigationDrawerActivityonCreate方法中。执行此操作时,Android将从您指定的Fragment开始,因此开头不为空。

相关问题