如何在导航抽屉中添加开关按钮

4
我希望在导航抽屉中使用开关按钮,以便在主布局中添加和删除片段。
这是我的代码 - `menuitem.xml`
<group
    android:id="@+id/drawer_group1"
    android:checkableBehavior="single">
    <item
        android:id="@+id/nav_timer"
        android:icon="@drawable/ic_timer"
        android:title="Timer">

    </item>

    <item
        android:id="@+id/addFragment_Bt"
        app:actionViewClass="android.widget.Switch"
        android:title="Most Used" />
    <item
        android:id="@+id/nav_settings"
        android:icon="@drawable/ic_settings"
        android:title="Settings">

    </item>
</group>`

MainActivity.class

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
    switch (menuItem.getItemId()) {
        switch (menuItem.getItemId()) {
            case R.id.addFragment_Bt:

                Switch switchCompat = findViewById(R.id.addMostUsed_Bt);
                switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        FragmentManager fragmentManager = getSupportFragmentManager();
                        Fragment fragment = fragmentManager.findFragmentById(R.id.Most_Used_Fragment_container);
                        if (isChecked == true) {
                            if (fragment != null) {
                                fragmentManager.popBackStack();
                            }

                        }
                    }
                });
                break;
        }
    }
    mDrawerLayout.closeDrawer(GravityCompat.START);
    return true;

    }
}

现在我只是试图删除已添加的片段。

3个回答

7

menuitem.xml

<item android:id="@+id/nav_switch"
            app:actionLayout="@layout/switch_menu"
            android:title="Send"
            android:icon="@drawable/ic_menu_send"/>

switch_menu 是用于切换的布局。

switch_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.SwitchCompat
        android:id="@+id/switch_id"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:text=""/>
</LinearLayout>

进入活动的访问开关:--

SwitchCompat switch_id;

switch_id =  actionView.findViewById(R.id.switch_id);
        switch_id.setChecked(true);
        switch_id.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), switch_id.isChecked()? "is checked!!!" : "not checked!!!",Toast.LENGTH_SHORT).show();
            }
        });

使用上述代码的输出为:

enter image description here

我希望它对你有用。


2
这对我很有用。
///这是菜单项。
<item
     android:id="@+id/darkModeMenu"
     android:title="Dark Mode"
     android:icon="@drawable/ic_darkmode"
     app:actionViewClass="androidx.appcompat.widget.SwitchCompat"
     ></item>

//将以下代码写入Activity的onCreate()方法中。
val menuItem = navigationView.menu.findItem(R.id.darkModeMenu)
    val switch_id = menuItem.actionView as SwitchCompat
    switch_id.setChecked(true)
    switch_id.setOnClickListener(View.OnClickListener {
        Toast.makeText(
            applicationContext,
            if (switch_id.isChecked()) "is checked!!!" else "not checked!!!",
            Toast.LENGTH_SHORT
        ).show()
    })

1

我尝试了这些方法,但都没有起作用。经过一个小时的研究,最终我找到了答案。

请将注意力集中在我的观点上。

首先,在activity_main_drawer中添加开关。这是您其他抽屉菜单项所在的位置。

       <item
            android:id="@+id/app_bar_switch"
            android:title="Switch"
            app:actionLayout="@layout/switch_item"
            app:showAsAction="always" />

接下来需要创建XML并添加@layout/switch_item。确保将id @+id/darkModeSwitch 添加到您的切换按钮上。如果XML是自动生成的,则只需将id添加到切换按钮即可。

<RelativeLayout
    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">
    <Switch
        android:id="@+id/darkModeSwitch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        tools:ignore="UseSwitchCompatOrMaterialXml" />
</RelativeLayout>

最后,添加以下Java代码

   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        // Code starts here
        NavigationView navigationView = binding.navView; // your navigation drawer id
        MenuItem menuItem = navigationView.getMenu().findItem(R.id.app_bar_switch); // first insialize MenuItem 
        @SuppressLint("UseSwitchCompatOrMaterialCode") Switch switchButton = (Switch) menuItem.getActionView().findViewById(R.id.darkModeSwitch); 
        // if you are using Switch in your @layout/switch_item then use Switch or use SwitchCompact
        switchButton.setOnCheckedChangeListener((compoundButton, b) -> {
            if (b){
                Toast.makeText(this, "True", Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(this, "False", Toast.LENGTH_SHORT).show();
            }
        });
}

我希望能对您有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接