如何在导航抽屉中更改图标大小

4
我想在导航抽屉中更改图标大小。 有人能告诉我如何实现这一点吗?以下是XML和Java文件。
 <?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>

活动类:

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();
    }
}

1
嗨,Parveen,欢迎来到SO。你能把你的问题简化成一个简单易懂的可重现的例子吗?在这里我们有点迷失在你的代码中了。请阅读MCVE - J. Chomel
我已经从Java和XML文件中简化了一些代码。 - Parveen Prajapati
dimen.xml 文件中覆盖 navigation_icon_size 属性。例如: <dimen name="navigation_icon_size">72dp</dimen> - Bona Fide
我已经尝试过了,但这并没有起作用,先生... - Parveen Prajapati
2个回答

12
您可以通过在dimens.xml中覆盖design_navigation_icon_size属性来更改导航抽屉图标的大小。
<dimen name="design_navigation_icon_size" tools:override="true">40dp</dimen>

6
更简单和清晰的做法是在NavigationView中设置itemIconSize。
<com.google.android.material.navigation.NavigationView
    android:id="@+id/navigationView"
    app:itemIconSize="32dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

这是一种更加简洁的方法。 - Simran Sharma

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