Android x PreferenceFragmentCompat 重叠 androidx.fragment.app.Fragment

Android x PreferenceFragmentCompat overlap androidx.fragment.app.Fragment

我有设置片段,它作为逻辑工作得很好,但每当我切换到 PreferenceFragmentCompat 它的布局背景变得透明,最后一个片段和设置片段布局相互重叠。

我有导航抽屉,我在其中设置了片段,它只发生在 PreferenceFragmentCompat 导航抽屉中的其他片段工作正常。

我试着在 PreferenceFragmentCompat XML 中设置背景颜色 android:background="?android:attr/colorBackground" 但它仍然重叠

主要Activity布局

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/Navigation_Drawer_Main">

    <!--The Main Layout For Content-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <!--Toolbar for actionbar   -->
        <include
            android:id="@+id/Navigation_Drawer_toolbar"
            layout="@layout/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <RelativeLayout
            android:id="@+id/Navigation_Main_Layout"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >


        </RelativeLayout>

    
    </LinearLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nvView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@android:color/white"
        app:menu="@menu/drawer_view" />


</androidx.drawerlayout.widget.DrawerLayout>

主要Activity片段切换

public void selectDrawerItem(MenuItem menuItem) {
        // Create a new fragment and specify the fragment to show based on nav item clicked
        Fragment fragment = null;


        switch (menuItem.getItemId()) {

            case R.id.HeaderImageView:
                fragment = new Edit();
                break;
            
            case R.id.noti_Fragment:
            
                fragment = new Notification();
                break;

            case R.id.add_Fragment:

                fragment = new AddMoney();

                break;

            case R.id.check_Fragment:
                fragment = new Check_Fragment();

                break;

            case R.id.setting_Fragment:
                Log.d(TAG, "Setting Fragment Pressed ");
                fragment = new Setting_NavigationDrawer();
                break;

            case R.id.help_Fragment:
                Log.d(TAG, "Help Fragment Pressed ");

                fragment = new Help_Fragment();

                break;

            case R.id.signOut_Fragment:
                new Session_Logout(this).execute();
                drawerLayout.closeDrawers();
                return;

        }


        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.Navigation_Main_Layout, fragment);
        fragmentTransaction.setCustomAnimations(R.animator.enter_anim, R.animator.exit_anim);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();

        // Highlight the selected item has been done by NavigationView

        menuItem.setChecked(true);

        // Set action bar title

        setTitle(menuItem.getTitle());

        // Close the navigation drawer

        drawerLayout.closeDrawers();

    }

首选项片段布局

<?xml version="1.0" encoding="utf-8"?>


<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="?android:attr/colorBackground"

    android:clickable="true"
    android:focusable="true">

    <PreferenceCategory
        android:iconSpaceReserved="false"
        android:key="app_setting"
        android:summary="General account setting for app"
        android:title="Account Setting">

        <SwitchPreferenceCompat
            android:checked="true"
            android:defaultValue="true"
            android:icon="@drawable/ic_location"
            android:key="locationButton"
            android:title="Location" />

        <Preference
            android:fragment="com.elaxer.Settings_Fragmets.BackUp_Fragment"
            android:icon="@drawable/ic_loop_black_24dp"
            android:title="Reset Account" />


    </PreferenceCategory>
    <PreferenceCategory android:title="Notifications">
        <SwitchPreferenceCompat
            android:defaultValue="true"
            android:icon="@drawable/ic_vibration_black_24dp"
            android:key="Notification_vibrate"
            android:summary="Vibrate when receive new notification"
            android:title="Vibrate" />
       

    </PreferenceCategory>


    <PreferenceCategory android:title="Privacy &amp; User Setting">
        <Preference
            android:icon="@drawable/ic_backup_black_24dp"
            android:key="bkup"
            android:summary="Backup account"
            android:title="Back up my account">

        </Preference>
        <PreferenceCategory
            android:icon="@drawable/ic_phonelink_lock_black_24dp"
            android:title="Privacy Control">

            <ListPreference
                android:defaultValue="1"
                android:dialogTitle="Post Privacy"
                android:entries="@array/pref_futurepost_entries"
                android:entryValues="@array/pref_futurepost_values"
                android:key="futurePot"
                android:summary="Select who can view your next post."
                android:title="Who Can See Your Future Post">

            </ListPreference>
        
            <ListPreference
                android:defaultValue="1"
                android:dialogTitle="Allow payment From"
                android:entries="@array/pref_futurepost_entries"
                android:entryValues="@array/pref_futurepost_values"
                android:key="Postpayment"
                android:title="Who Can payment On Your Proudct">

            </ListPreference>
            <CheckBoxPreference
                android:defaultValue="true"
                android:key="dob"
                android:summary="Check to show year of opening"
                android:title="Show Complete Data Of opening " />
            <CheckBoxPreference
                android:defaultValue="true"
                android:key="email"
                android:title="Show Email Publicly " />
            <CheckBoxPreference
                android:defaultValue="true"
                android:key="distance"
                android:title="Show My Approx pricing" />
            <CheckBoxPreference
                android:defaultValue="true"
                android:key="status"
                android:title="Show My Status " />
            <CheckBoxPreference
                android:defaultValue="true"
                android:key="directMessage"
                android:title="Allow Message From Customer" />
        </PreferenceCategory>
        

    </PreferenceCategory>




</PreferenceScreen>

在您的设置片段中,添加:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = super.onCreateView(inflater, container, savedInstanceState);
    view.setBackgroundColor(getResources().getColor(android.R.color.white));
    return view;
}