如何更改工具栏名称并在工具栏中添加图标

4

我刚刚使用Android Studio导航视图模板制作了这个应用程序。一切都正常工作,但我有以下两个要求:

  1. 我想将工具栏标题居中。
  2. 我想将图像放在标题旁边。

这是我的XML代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.abdulsalam.lahoreqalander.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<!--<android.support.design.widget.FloatingActionButton-->
    <!--android:id="@+id/fab"-->
    <!--android:layout_width="wrap_content"-->
    <!--android:layout_height="wrap_content"-->
    <!--android:layout_gravity="bottom|end"-->
    <!--android:layout_margin="@dimen/fab_margin"-->
    <!--android:src="@android:drawable/ic_dialog_email" />-->

问题:

我不知道标题字符串存储在哪里,我已经检查了string.xml文件,里面有应用程序名称并且正好显示在工具栏标题上。

所以,请问有没有建议可以更改标题字符串而不更改应用程序名称,事实上我想自定义它,并且如何将图像放在旁边。

请注意,我正在使用由Android Studio创建的模板。

.


获取操作栏().设置标题("Material Design 示例"); - Suhas Bachewar
getSupportToolBar()可以帮助您设置标题和可绘制项。 - Ajay Pandya
但是如何设置重力呢? - Allay Khalil
你需要创建自己的工具栏样式,设置可绘制而不是颜色。https://dev59.com/JF0b5IYBdhLWcg3wM-8c#29533038 - Ajay Pandya
而且,将其以设计方式处理在XML内部是否更好?我想知道在Java代码和XML中都没有为标题设置字符串,但是它是如何从该字符串中获取标题的。 - Allay Khalil
3个回答

4
在您的Activity类中,在onCreate方法中执行以下操作:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle("example");
toolbar.setLogo(R.drawable.icon);

或者编辑字符串资源并更改标题值和图标。


编辑字符串资源时,我可以看到设置为其的哪个字符串资源,如代码所示。 - Allay Khalil
在Java中也没有改变重力的方法。 - Allay Khalil
在清单文件中检查该活动是否附带标签。 - Avinash Joshi
找不到getSupportToolbar。 - Allay Khalil

4
您可以在XML中这样实现。
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay">

    <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     android:layout_gravity="center">

          <ImageView
            android:src="@mipmap/ic_launcher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

          <TextView
            style="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"
            android:text="@string/app_name"
            android:background="?attr/selectableItemBackground"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

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

1

首先来自文档:

在API 21设备及更高版本上,不建议使用应用程序图标加标题作为标准布局。

要通过xml设置图标,您可以在工具栏中使用android:navigationIcon。要设置文本,我也会采用Java代码方法,因为我在文档中找不到任何有关xml的内容。无论如何,文本都不会居中。

如果您想将文本居中,则必须修改您的工具栏。工具栏是一个视图,您可以在其中放置任何想要的内容。

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingEnd="@dimen/activity_horizontal_margin"         
        android:id="@+id/toolbar_content">

        // define text and icon here

    </RelativeLayout>

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

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