点击导航抽屉中的项目文本会加粗

3
我有一个导航抽屉,当我在导航视图中点击任何项目时,我的前五个项目文本都会变成粗体。我尝试删除片段调用,但仍然所有文本都变成了粗体。无论我点击哪个项目,它都会将前五个项目变成粗体。

enter image description here

public class Navigation_Drawer extends AppCompatActivity {

    
    Toolbar toolbar;
    DrawerLayout drawerLayout;
    NavigationView navigationView;
    //RelativeLayout mainScreen;
    ActionBarDrawerToggle drawerToggle;
    




    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       

        setContentView(R.layout.navigation_drawer);
        
        //Initialise Views
        drawerLayout = findViewById(R.id.Navigation_parent);
        navigationView = findViewById(R.id.nvView);

        setupToolbar();
        navigationView.setItemIconTintList(null);
        setupDrawerContent(navigationView);
        settingHeaderItems();
        drawerToggle = setupDrawerToggle();
        drawerToggle.setDrawerIndicatorEnabled(true);
        drawerLayout.addDrawerListener(drawerToggle);
        


    }

   

    public void setupToolbar() {
        toolbar = findViewById(R.id.Navigation_Drawer_toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        getSupportActionBar().setHomeButtonEnabled(true);
    }

    private ActionBarDrawerToggle setupDrawerToggle() {
        // NOTE: Make sure you pass in a valid toolbar reference.  ActionBarDrawToggle() does not require it
        // and will not render the hamburger icon without it.
        //return new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open,  R.string.drawer_close);
        return new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);
    }

    public void setFragmentTitle(String title) {
        setTitle(title);
    }


    private void setupDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                        Navigation_Drawer.this.selectDrawerItem(menuItem);
                        Log.d(TAG, "onNavigationItemSelected: Menu Item = "+menuItem);
                        return true;
                    }
                });

    }



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

        menuItem.setChecked(true);
        setTitle(menuItem.getTitle());

        switch (menuItem.getItemId()) {

            case 1:
                fragment = new EditProfile();
                break;
            case 2:
                Log.d(TAG, "Home Fragment Pressed ");
                getSupportFragmentManager().popBackStack(null, android.app.FragmentManager.POP_BACK_STACK_INCLUSIVE);
                ChangeFragment_ViewPager(0, false);
                // Highlight the selected item has been done by NavigationView
                // Set action bar title
                setTitle(menuItem.getTitle());
                // Close the navigation drawer
                drawerLayout.closeDrawers();
                return;

            case 3:
                Log.d(TAG, "PPL Fragment Pressed ");
                getSupportFragmentManager().popBackStack(null, android.app.FragmentManager.POP_BACK_STACK_INCLUSIVE);

                ChangeFragment_ViewPager(1, false);
                // Highlight the selected item has been done by NavigationView

                // Set action bar title
                setTitle(menuItem.getTitle());
                // Close the navigation drawer
                drawerLayout.closeDrawers();
                return;

            case 4:
                Log.d(TAG, "4 Fragment Pressed ");
                fragment = new 4();
                break;

            case 5:
                Log.d(TAG, "5 Fragment Pressed ");
                fragment = new 5();
                break;

            case 6:
                Log.d(TAG, "6 Fragment Pressed ");
                //fragmentClass = PPL_main.class;
                
                String link = " ";
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_TEXT, link);

                startActivity(Intent.createChooser(intent, "Invite Friends"));
                // Highlight the selected item has been done by NavigationView
                // Set action bar title
                // Close the navigation drawer
                return;

            case 7:
                Log.d(TAG, "Interest Fragment Pressed ");
                fragment = new 7();
                break;

            case 8:
                Log.d(TAG, "Setting Fragment Pressed ");
                fragment = new 8();
                break;

            case 9:
                Log.d(TAG, "Help Fragment Pressed ");
                //fragmentClass = PPL_main.class;
                fragment = new 9();
                
                break;

            case R.id.signOut_Fragment:
                                
                return;
        }


        // Close the navigation drawer
        drawerLayout.closeDrawers();
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.parentFragment, fragment.getClass(),null);
        fragmentTransaction.setCustomAnimations(R.anim.enter_anim, R.anim.exit_anim);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
        
    }

    private void settingHeaderItems() {
        View HeaderLayout = navigationView.inflateHeaderView(R.layout.navigation_header_image);
        //Main Screen Tabs With VIew Pager
        headerImage = HeaderLayout.findViewById(R.id.HeaderImageView);
        headerImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.Navigation_Main_Layout, new EditProfile());
                fragmentTransaction.setCustomAnimations(R.anim.enter_anim, R.anim.exit_anim);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
                drawerLayout.closeDrawers();
            }
        });
        headerUserImage = HeaderLayout.findViewById(R.id.Header);
        userName = HeaderLayout.findViewById(R.id.ViewText);
        userViews = HeaderLayout.findViewById(R.id.Views);
        if (App_Functions.coverPath.equals("Invalid Image")) {
            headerImage.setBackgroundResource(R.drawable.cam_icon);
        } else {
            Log.d(TAG, "Path Of Cover Photo " + App1);
            Glide.with(this).load(App2)
                    .diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true)
                    .into(headerImage);
            //  Glide.with(context).load(App_Functions.coverPath).apply(new RequestOptions().skipMemoryCache(true).onlyRetrieveFromCache(false).diskCacheStrategy(DiskCacheStrategy.NONE)).into(holder.HeaderImage);
        }
        Bitmap bitmap = BitmapFactory.decodeFile(App2);

        userName.setText(Session.getU());
       
        Glide.with(this).load(App2)
                .diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true)
                .into(headerUserImage);

    }
  

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        drawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        drawerToggle.onConfigurationChanged(newConfig);
    }

  
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

  
} // END OF NAVIGATION DRAWERS

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

    <!--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" />
        <FrameLayout
            android:id="@+id/Navigation_Main_Layout"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >



       

            <androidx.viewpager2.widget.ViewPager2
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/Navigation_Drawer_ViewPager">

            
            </androidx.viewpager2.widget.ViewPager2>

        </FrameLayout>
    </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>

1
遇到了完全相同的问题。当我点击任何项时,前5个项目的文本样式都会设为粗体。也许这是我们正在使用的材料库中的一个错误? - Harshal Pudale
2个回答

1
我通过在NavigationView中应用样式(itemTextAppearance)来解决了相同的问题。
<com.google.android.material.navigation.NavigationView
        ...
        app:itemTextAppearance="@style/Style_TextView" // You have to add this line
        ...
/>

themes.xml中添加以下样式。
<style name="Style_TextView">
        <item name="android:textStyle">normal</item>
</style>

-1

这个答案展示了如何改变文本的大小。你应该能够通过添加来重置项目的字体重量

<item name="android:textStyle">normal</item>

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