Android-将工具栏的标题居中 [英] Android - Center the title of the Toolbar

查看:93
本文介绍了Android-将工具栏的标题居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个这样的工具栏:

 <?xml version ="1.0" encoding ="utf-8"?>< android.support.v7.widget.Toolbar xmlns:android ="http://schemas.android.com/apk/res/android"xmlns:local ="http://schemas.android.com/apk/res-auto"android:id ="@ + id/toolbar"android:layout_width ="match_parent"android:layout_height ="wrap_content"android:minHeight =?attr/actionBarSize"android:background =#263355"local:theme ="@ style/ThemeOverlay.AppCompat.Dark.ActionBar"local:popupTheme ="@ style/ThemeOverlay.AppCompat.Light"/> 

我将标题添加为:

  getSupportActionBar().setTitle(title); 

我已阅读 Android工具栏中心标题和自定义字体

但是,如果我更改工具栏的xml,我不知道如何更改TextView的值.有人可以帮助我吗?

解决方案

如果您具有自定义的工具栏布局,请不要使用 getSupportActionBar().setTitle(title); 设置标题./p>

相反,假设您的XML布局如下所示:

 <!-工具栏->< android.support.v7.widget.Toolbarandroid:id ="@ + id/main_toolbar"android:layout_width ="match_parent"android:layout_height ="wrap_content">< LinearLayoutandroid:id ="@ + id/main_toolbar_layout"android:layout_width ="match_parent"android:layout_height ="wrap_content"android:gravity ="center"android:orientation ="vertical">< TextViewandroid:id ="@ + id/main_toolbar_title"android:layout_width ="wrap_content"android:layout_height ="wrap_content"/></LinearLayout></android.support.v7.widget.Toolbar> 

您可能需要在您的 MainActivity onCreate()中的某个地方打电话:

 (((TextView)findViewById(R.id.main_toolbar_title)).setText("Title!"); 

i have a Toolbar in my app like that:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="#263355"
    local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

And i add the title as that:

getSupportActionBar().setTitle(title);

I've read that Android toolbar center title and custom font

But if i change the xml of the toolbar, i don't know how to change the value of the TextView. Someone can help me ?

解决方案

Do not use getSupportActionBar().setTitle(title); to set the title, if you have a custom Toolbar layout.

Instead, assuming your XML layout looks like this:

        <!-- Toolbar -->
        <android.support.v7.widget.Toolbar
            android:id="@+id/main_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/main_toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/main_toolbar_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </LinearLayout>

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

You need to call, somewhere in your MainActivity's onCreate() probably:

((TextView) findViewById(R.id.main_toolbar_title)).setText("Title!");

这篇关于Android-将工具栏的标题居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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