如何在底部导航中包含设置/首选项

时间:2021-01-07 05:29:00

标签: java android xml android-fragments

我正在尝试将 settings.xml 包含到我的底部导航中,但我无法获取设置片段来查看 settings.xml;因为我在底部导航中包含了设置。

MainActivity.java

package lng.techservices.taskit;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.os.Bundle;
import android.view.MenuItem;

import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
        bottomNav.setOnNavigationItemSelectedListener(navListener);

        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                new AlarmsFragment()).commit();
    }

    private BottomNavigationView.OnNavigationItemSelectedListener navListener =
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    Fragment selectedFragment = new TasksFragment();

                    switch (item.getItemId()) {
                        case R.id.nav_tasks:
                            selectedFragment = new TasksFragment();
                            break;
                        case R.id.nav_alarms:
                            selectedFragment = new AlarmsFragment();
                            break;
                        case R.id.nav_settings:
                            selectedFragment = new SettingsFragment();
                            break;
                    }
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                            selectedFragment).commit();

                    return true;
                }
            };
}

(布局)activity_main.xml

<?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:background="@color/primaryDark"
    tools:context=".MainActivity">
    
    <fragment
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottom_navigation"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:menu="@menu/bottom_navigation"
        app:itemIconTint="@color/white"
        app:itemTextColor="@color/white"
        android:background="@color/primaryDarkAccent"/>

</RelativeLayout>

(xml) settings.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <SwitchPreference
        android:defaultValue="false"
        android:key="NightMode"
        android:summaryOff="Night mode is off"
        android:summaryOn="Night mode is on"
        android:title="@string/title_NightMode" />

    <ListPreference
        android:defaultValue="false"
        android:entries="@array/items_name"
        android:entryValues="@array/items_value"
        android:key="ORIENTATION"
        android:title="@string/title_orientation" />
</PreferenceScreen>

(布局)fragment_alarms.xml、fragment_settings.xml、fragment_task.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/primaryDark">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment"
        android:textSize="30sp"
        android:layout_centerInParent="true"/>

</RelativeLayout>

(菜单)bottom_navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/nav_tasks"
        android:icon="@drawable/ic_tasks_black_24dp"
        android:title="string/title_tasks" />

    <item
        android:id="@+id/nav_alarms"
        android:icon="@drawable/ic_alarms_black_24dp"
        android:title="@string/title_alarms" />

    <item
        android:id="@+id/nav_settings"
        android:icon="@drawable/ic_settings_black_24dp"
        android:title="@string/title_settings" />

</menu>

0 个答案:

没有答案