如何将工具栏的徽标置于右侧? [英] How put the logo of toolbar at right?

查看:75
本文介绍了如何将工具栏的徽标置于右侧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这样做是为了膨胀"工具栏上的徽标:

I did this to "inflate" the logo at the toolbar:

toolbar.setNavigationIcon(R.drawable.navigationIcon);
toolbar.getNavigationIcon().setTint(Color.BLUE);
toolbar.setLogo(R.drawable.logo);
toolbar.getLogo().methodForFloatRightHere(); 

是否存在一些不在视图上使用膨胀"的简单方法?

Does there exist some simple method that doesn't use "inflate" on a view?

推荐答案

您可以将自定义视图添加到工具栏.

You can add custom views to a toolbar.

关键是徽标 ImageView 上的 android:layout_gravity ="center" .

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?actionBarSize"
    app:contentInsetEnd="@dimen/material_content_keyline"
    app:contentInsetStart="@dimen/material_content_keyline"
    app:navigationContentDescription="@string/abc_action_bar_up_description"
    app:navigationIcon="@drawable/abc_ic_ab_back_mtrl_am_alpha">

    <ImageView
        android:id="@+id/toolbar_logo"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:src="@drawable/logo"/>
</android.support.v7.widget.Toolbar>

在手机上,键线是72dp,在平板电脑上是80dp.这样可以确保添加到工具栏的所有视图(导航/折叠图标和菜单项除外)都位于矩形的中心,该矩形至少从左和右居中72/80dp.如果您有更多菜单项并且徽标向左倾斜,则增加该数字.

Keyline is 72dp on phones, 80dp on tablets. This ensures any views added to toolbar (besides navigation/collapse icon and menu items) are centered in a rectangle at least 72/80dp from left and right. Increase if you have more menu items and the logo leans to the left.

如果您不想使用XML:

If you don't want to use XML:

mToolbar.addView(mLogo,
    new Toolbar.LayoutParams(Toolbar.LayoutParams.WRAP_CONTENT, Toolbar.LayoutParams.WRAP_CONTENT, Gravity.CENTER));

这篇关于如何将工具栏的徽标置于右侧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆