如何用 Android 工具栏中的徽标替换标题?
How to replace title by logo in Android Toolbar?
我想用像 Twitter App 这样的徽标替换工具栏中的标题。
我认为我们可以像这样以编程方式替换它:
mToolbar = (Toolbar) findViewById(R.id.app_toolbar);
mToolbar.setLogo(R.drawable.ic_logo);
但是我想在我的XML工具栏声明中直接替换它,但是我看不到属性app:logo
或android:logo
。
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/app_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/MyCustomToolBarTheme" />
请帮帮我,谢谢。
如果您查看 Toolbar
and Toolbar
's custom attributes 的源代码,您可以使用几个属性来自定义 Toolbar
,但徽标不是其中之一:
- 标题文本外观
- 字幕文本外观
- 导航图标
- ...
Toolbar.setLogo(resId)
似乎是目前唯一的选择。
工具栏包含一个 ViewGroup,您可以在其中填充您想要的任何视图 --- 如果您想要徽标,可以在其中添加 ImageView。示例:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/ic_logo"/>
</android.support.v7.widget.Toolbar>
如果您手动将其附加到 AppCompatActivity 或 ActionBar activity,它 "becomes" 一个 ActionBar,然后必须使用这些代码函数进行操作。
我想用像 Twitter App 这样的徽标替换工具栏中的标题。
我认为我们可以像这样以编程方式替换它:
mToolbar = (Toolbar) findViewById(R.id.app_toolbar);
mToolbar.setLogo(R.drawable.ic_logo);
但是我想在我的XML工具栏声明中直接替换它,但是我看不到属性app:logo
或android:logo
。
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/app_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/MyCustomToolBarTheme" />
请帮帮我,谢谢。
如果您查看 Toolbar
and Toolbar
's custom attributes 的源代码,您可以使用几个属性来自定义 Toolbar
,但徽标不是其中之一:
- 标题文本外观
- 字幕文本外观
- 导航图标
- ...
Toolbar.setLogo(resId)
似乎是目前唯一的选择。
工具栏包含一个 ViewGroup,您可以在其中填充您想要的任何视图 --- 如果您想要徽标,可以在其中添加 ImageView。示例:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/ic_logo"/>
</android.support.v7.widget.Toolbar>
如果您手动将其附加到 AppCompatActivity 或 ActionBar activity,它 "becomes" 一个 ActionBar,然后必须使用这些代码函数进行操作。