中心的标题栏

时间:2017-04-19 14:04:45

标签: android android-titlebar

如何在中心获得标题栏?我想把它完全放在我画在下面的图片上的蓝线上。提前致谢

这是我的代码:

触摸:

     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>

我的输出: 我希望它完全在我在下面的图片上绘制的蓝线上。

I want its exactly on the blue line that I drew on the picture below.

2 个答案:

答案 0 :(得分:0)

使用RelativeLayout代替LinearLayoutandroid: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>

答案 1 :(得分:0)

只需创建自定义工具栏。将重力添加到textview

$

活动

<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>