Строка заголовка в центре

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

Вот мой код:

На ощупь:

     getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
       getSupportActionBar().setCustomView(R.layout.abs_layout);

XML:

     <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical" android:layout_width="match_parent"
     android:layout_height="match_parent">

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:text="Home"
    android:textColor="#ffffff"
    android:id="@+id/mytext"
    android:textSize="18sp" />

   </LinearLayout>

Мой вывод: я хочу, чтобы он был точно на синей линии, которую я нарисовал на картинке ниже.

Я хочу, чтобы он был точно на синей линии, которую я нарисовал на картинке ниже

2 ответа

Использование RelativeLayout вместо LinearLayout, android:layout_gravity не работает с LinearLayout,

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:text="Home"
        android:textColor="#ffffff"
        android:id="@+id/mytext"
        android:textSize="18sp" />

</RelativeLayout>

Просто создайте пользовательскую панель инструментов. добавить тяжести в текстовое представление

<android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/mypay_purple">

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                style="@style/FontNormal"
                android:textColor="@color/mypay_white"
                android:text="Title"
                />

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

в деятельности

setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    title.setText("Payment");
}
Другие вопросы по тегам