Установить текст заголовка ActionBar по центру - PullRequest
0 голосов
/ 24 мая 2018

Как мне установить текст панели действий в центр.Я использовал код ниже, чтобы установить текст по центру:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center">


  <TextView
       android:id="@+id/title"
       android:layout_width="match_parent"
       android:text="Example"
       android:layout_height="wrap_content"
       android:layout_centerInParent="true"
       android:ellipsize="end"
       android:maxLines="1"
       android:textAppearance="?android:attr/textAppearanceMedium"
       android:textColor="@color/white"
       android:gravity="center" />

</RelativeLayout>

Активность:

ActionBar.SetDisplayOptions(ActionBarDisplayOptions.ShowCustom, ActionBarDisplayOptions.ShowCustom);

LayoutInflater inflater = (LayoutInflater)GetSystemService(Context.LayoutInflaterService);
View v = inflater.Inflate(Resource.Layout.layout_actionbar_centerTitle, null);

ActionBar.LayoutParams p = new ActionBar.LayoutParams(
        ViewGroup.LayoutParams.MatchParent,
        ViewGroup.LayoutParams.MatchParent,
       GravityFlags.Center);

var title = ((TextView)v.FindViewById(Resource.Id.title));
title.Text = AgliveResource.ContactUs;

ActionBar.SetCustomView(v, p);
ActionBar.SetDisplayShowTitleEnabled(true);
ActionBar.SetDisplayHomeAsUpEnabled(true);

Это прекрасно работает, когда у меня есть меню гамбургера справа вместе со спинойзначок слева.Но есть некоторые экраны, которые не требуют меню гамбургера, в то время как текст не отображается правильно в центре.

Изображение 1 с меню: enter image description here Изображение 2 без меню: enter image description here

Ответы [ 2 ]

0 голосов
/ 24 мая 2018

Возможно, вам нужно изменить ширину текста android:layout_width="match_parent" на android:layout_width="wrap_content"

<RelativeLayout
        android:layout_width="match_parent"
        android:id="@+id/navBar"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@color/app_color_Brown">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/backBtn"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:src="@drawable/back_icon" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="20dp"
            android:paddingBottom="20dp"
            android:layout_centerInParent="true"
            android:text="Privacy Policy"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            />
    </RelativeLayout>
0 голосов
/ 24 мая 2018

Если вы не имеете меню гамбургера, укажите отступ в 56 дп.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="56dp"
android:gravity="center">


  <TextView
       android:id="@+id/title"
       android:layout_width="match_parent"
       android:text="Example"
       android:layout_height="wrap_content"
       android:layout_centerInParent="true"
       android:ellipsize="end"
       android:maxLines="1"
       android:textAppearance="?android:attr/textAppearanceMedium"
       android:textColor="@color/white"
       android:gravity="center" />

</RelativeLayout>
...