Android 如何使用工具栏更改工具栏名称和放置图标

Android 如何使用工具栏更改工具栏名称和放置图标,android,android-studio,android-toolbar,Android,Android Studio,Android Toolbar,我刚刚使用android studio导航视图模板制作了这个应用程序。这一切工作正常,但我有以下两个要求 我想将工具栏标题居中 我想把图片放在标题旁边 以下是我的xml: <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ap

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

  • 我想将工具栏标题居中
  • 我想把图片放在标题旁边
  • 以下是我的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制作的模板


    在活动类中,在创建时执行此操作

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setTitle("example");
    toolbar.setLogo(R.drawable.icon);
    

    或者编辑字符串资源并更改标题值和图标,您可以这样在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>
    
    
    
    首先来自:

    在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>
    
    
    //在此定义文本和图标
    
    getSupportActionBar().setTitle(“材料设计样本”);getSupportToolBar()将帮助您获得标题和可绘制性,但如何设置重力您必须为工具栏创建自己的样式,设置可绘制性,而不是颜色,并且最好在xml内部以设计方式处理,我想知道在java和xml代码中,没有为标题设置字符串,但如何从该字符串中获取标题要编辑字符串资源,我可以看到哪个字符串资源被设置为它,如代码所示,在java中也无法更改清单文件中的重力检查活动将伴随标签GetSupportToolbar无法找到