2

С левой стороны моей панели инструментов у меня есть логотип, и я хочу сосредоточить TextView на той же панели инструментов, чтобы действовать как мой заголовок. По какой-то причине название не центрируется, а вместо этого справа. Вот моя попытка:Android - Как центрировать заголовок (TextView) на панели инструментов?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context=".Register"> 

<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/sign_up_bar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="?attr/colorPrimary" 
    android:fitsSystemWindows="true" 
    app:contentInsetLeft="5dp" 
    app:contentInsetStart="0dp"> 

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

     <ImageView 
      android:id="@+id/toolbar_icon" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:adjustViewBounds="true" 
      android:maxHeight="60dp" 
      android:maxWidth="60dp" 
      android:scaleType="fitCenter" 
      android:src="@mipmap/icon" /> 

     <TextView 
      android:id="@+id/toolbar_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:text="Sign Up" 
      android:textColor="#ffffff" 
      android:textSize="21dp" 
      android:textStyle="bold" /> 
    </LinearLayout> 
</android.support.v7.widget.Toolbar> 
... 
</LinearLayout> 

Код:

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Typeface myCustomFont = Typeface.createFromAsset(getAssets(), "fonts/scss.ttf"); 
     setContentView(R.layout.frag_register_name); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.sign_up_bar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#3f9845"))); 
     getSupportActionBar().setDisplayShowTitleEnabled(false); 

Кто-нибудь знает, почему название не центрирования на панели инструментов? Любая помощь высоко ценится.

ответ

1

Существует несколько способов решить вашу проблему. Самый быстрый - изменить TextView следующим образом.

<TextView 
      android:id="@+id/toolbar_title" 
      android:layout_width="match_parent" // Fill parent 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:text="Sign Up" 
      android:gravity="center" // Make the text center aligned 
      android:textColor="#ffffff" 
      android:textSize="21dp" 
      android:textStyle="bold" /> 
    </LinearLayout> 

Это гарантирует, что ваш текст будет охватывать все пространство рядом со значком и центрировать текст внутри него. Для этого вам даже не нужен LinearLayout. Его можно удалить, и он все равно будет работать.

Смежные вопросы