简体   繁体   中英

How do i change the icon size in navigation drawer

I want to change the icon size in navigation drawer. Could anyone tell me, how can i achieve this. here is the xml and java file.

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

<group android:id="@+id/title1" android:checkableBehavior="single">
    <item
        android:id="@+id/homepage"
        android:icon="@mipmap/homemenu"
        android:title="Home" />
    </group>

<group android:id="@+id/title2" android:checkableBehavior="single">
    <item
        android:id="@+id/foodpage"
        android:icon="@mipmap/foodmenu"
        android:title="Food" />
    </group>

<group android:id="@+id/title6" android:checkableBehavior="single">
    <item
        android:id="@+id/exit"
        android:icon="@mipmap/exitmenu"
        android:title="Exit" />
</group>

Activity class:

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        setFrameVisibility(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.colourred) {
            getWindow().getDecorView().setBackgroundColor(Color.parseColor("#33CCCC"));
            return true;
        } else if(id == R.id.colourblue){
            getWindow().getDecorView().setBackgroundColor(Color.parseColor("#CC9900"));

        }

        return super.onOptionsItemSelected(item);
    }


    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        FragmentManager fragmentManager = getFragmentManager();

        if (id == R.id.homepage) {
            Intent homepage = new Intent (MainActivity.this, MainActivity.class);
            startActivity(homepage);
                            // Handle the camera action
        } else if (id == R.id.foodpage) {
            //handle the food page here
            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame
                            , new FirstFragment())
                    .commit();

        } else if (id == R.id.schedulepage) {
            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame
                    , new ScheduleFragment())
                    .commit();

        } else if (id == R.id.emotionspage) {
            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame
                    , new EmotionsFragment())
                    .commit();

        } else if (id == R.id.basicneedspage) {
            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame
                    , new BasicneedsFragment())
                    .commit();

        } else if (id == R.id.exit) {
            askBeforeExit();

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    public void onBackPressed() {

        askBeforeExit();
    }
}

您可以通过覆盖 dimens.xml 中的 design_navigation_icon_size 属性来更改导航抽屉图标的大小

<dimen name="design_navigation_icon_size" tools:override="true">40dp</dimen>

An easier and cleaner approach would be to set the itemIconSize in the NavigationView.

<com.google.android.material.navigation.NavigationView
    android:id="@+id/navigationView"
    app:itemIconSize="32dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM