在左侧更改ToolBar的默认图标

8

我目前使用一个工具栏,并希望用一个主页按钮替代默认的返回按钮。但是,当我添加项目时,它总是添加到右侧。我也没有看到任何可选择的layout_gravity选项。是否有方法可以解决这个问题?

MainActivity

Toolbar toolbar;

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

    toolbar = (Toolbar)findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("                Testing");
    getSupportActionBar().setDisplayHomeAsUpEnabled(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.home_icon_empty, menu);
    return true;
}

home_icon_empty.xml

<item
    android:id="@+id/homepage"
    android:orderInCategory="100"
    android:title="HOME"
    android:icon="@drawable/home_icon"
    app:showAsAction="always"
    />

<item
    android:id="@+id/homepage1"
    android:orderInCategory="200"
    android:title="HOME"
    android:icon="@drawable/home_icon"
    app:showAsAction="always"
    />

activity_main.xml

<include
        android:id="@+id/app_bar"
        layout="@layout/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

app_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary">

</android.support.v7.widget.Toolbar>

当前工具栏输出结果:

在此输入图片描述

期望的工具栏输出结果:

在此输入图片描述

新编辑后的图像:

在此输入图片描述

4个回答

15

设置为导航图标:

Drawable drawable = ContextCompat.getDrawable(context, R.drawable.your_drawable)
toolbar.setNavigationIcon(drawable);
setSupportActionBar(toolbar);

它可以工作,但是图像大小没有像添加项目时那样正确调整。有什么解决方法吗?附加了参考图像。 - cubeb
这是因为您的可绘制对象具有比您期望的更低的分辨率(大小)。在材料设计中,菜单项建议为24dp。您可以通过编程方式调整其大小,或者在XML中将一些ImageView放置在<Toolbar>内。 - Deividi Cavarzan
我看到了你的图像。你的图标对于分辨率来说太大了。你需要创建一个更小的图标。你可以使用这个工具来完成:https://romannurik.github.io/AndroidAssetStudio/icons-actionbar.html - Deividi Cavarzan
链接有帮助吗?创建一个新的操作栏图标并替换,应该可以解决问题。 - Deividi Cavarzan
那我应该将其调整为48dp x 48dp吗? - cubeb
这是 Material Design 推荐的尺寸...但由你决定 =) - Deividi Cavarzan

2
您还可以:
mToolbar.setLogo( yourLogoID );

或者你可以:
mToolbar.setBackground( ContextCompat.getDrawable( yourContext, urBackgroundID );

where

mToolbar = yourToolbar;
mToolbar = findViewById(R.id.your_toolbar);
// or, if your toolbar get setted by a superclass
mToolbar = (Toolbar) getSupportActionBar();

你的样式应该是:Blablabla.NoActionBar,然后在xml布局中包含你的工具栏

如果你正在使用一个抽屉来显示左侧菜单,当向左滑动或点击主页按钮时,那么你应该使用上述方法之一(如果你不想超级类所有DrawerLayout lul)

bb


1

试试这个

toolbar.setNavigationIcon(R.drawable.your_icon);

在设置支持操作栏之前使用它。 不要调用此函数。

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

或者至少做到

getSupportActionBar().setDisplayHomeAsUpEnabled(false);

试一试。其中一些将会起作用。


它可以工作,但是图像大小没有像添加项目时那样正确调整。有什么解决方法吗?附加参考图像。 - cubeb

0

按照Material design guide中的尺寸添加您的可绘制资源,并在Activity onCreate中调用以下方法。

private void setUpToolBar() {
            setSupportActionBar(toolbar);
            //Set Home screen icon
            getSupportActionBar().setHomeAsUpIndicator(R.drawable.home_screen_icon);

            //Enable Home icon
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

            //Toolbar title
            getSupportActionBar().setTitle(getString(R.string.app_name));

           //Set toolbar as action bar
            setSupportActionBar(toolbar);
        }

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