How to set the ActionBar title in middle instead of the default left aligned position?

感情迁移 提交于 2020-01-27 08:44:46

问题


Trying to set the ActionBar title in middle instead of the default left aligned position. To do so, based on other answers this is what I've done:

    Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbarDownloads);
    setSupportActionBar(myToolbar);
    if(getSupportActionBar()!=null)
    {
        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        getSupportActionBar().setCustomView(R.layout.actionbar_layout);
    }

actionbar_layout.xml

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Downloads"
        android:layout_centerInParent="true"
        android:textColor="#000000"
        android:id="@+id/mytext"
        android:textSize="18sp" />

</RelativeLayout>

However, this does not produce the intended result (ie this renders the text however still is left aligned), what's wrong with this approach and how to fix this?


回答1:


try this use custom Toolbar

<android.support.v7.widget.Toolbar
    android:id="@+id/ar_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:layout_scrollFlags="exitUntilCollapsed"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Center"
        android:orientation="vertical">

        <TextView
             android:id="@+id/toolbar_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center" />
    </LinearLayout>

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

now in java file

private Toolbar toolbar;
toolbar = (Toolbar) findViewById(R.id.ar_toolbar);
TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
 mTitle.setText("Nilesh Rathod");
setSupportActionBar(toolbar);



回答2:


Simple approach use android:layout_gravity="center" on your TextView and hide default title using below code after that initialize your TextView and set value

Inside your activity:

setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
toolbar_title = (TextView) findViewById(R.id.toolbar_title);
toolbar_title.setText("My Custom center title");

Custom toolbar XML Code:

<?xml version="1.0" encoding="utf-8"?>
<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/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:contentInsetEnd="0dp"
    app:contentInsetLeft="0dp"
    app:contentInsetRight="0dp"
    app:contentInsetStart="0dp"
    app:contentInsetStartWithNavigation="0dp">

    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Toolbar Title" />

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



回答3:


You need to use Toolbar in your xml file and you can use TextView inside Toolbar. You can add custom view into Toolbar. Once you done with xml file then you can define Toolbar into Activity class.

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="58dp"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:titleTextColor="#ffffff">

        <TextView
            android:id="@+id/mytext"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Downloads"
            android:textColor="#000000"
            android:textSize="18sp" />

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

   //Rest of your code

</LinearLayout>

In Activity class you can define as

Toolbar toolbar;
toolbar = (Toolbar) findViewById(R.id.ar_toolbar);
toolbar.setTitle("");



回答4:


You don't need a Toolbar for this. Just use a theme like Theme.AppCompat.Light.DarkActionBar with ActionBar in your activity and use the following code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        getSupportActionBar().setCustomView(R.layout.actionbar_layout);
    }
    //your code
}



回答5:


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

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:minHeight="?attr/actionBarSize"
        app:contentInsetStart="0dp">

        <TextView
            android:id="@+id/back_txt"
            style="@android:style/TextAppearance.Medium"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:gravity="center"
            android:text="Back" />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Downloads"
            android:layout_gravity="center|center_horizontal|center_vertical"
            android:layout_marginTop="20dp"
            android:layout_centerInParent="true"
            android:textColor="#000000"
            android:id="@+id/mytext"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/filter_btn"
            style="@style/Base.TextAppearance.AppCompat.Medium"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|center_vertical|center_horizontal"
            android:layout_margin="10dp"
            android:drawablePadding="5dp"

            android:gravity="center"
            android:text="Back"
            android:textColor="@android:color/black" />
    </android.support.v7.widget.Toolbar>


</LinearLayout>


来源:https://stackoverflow.com/questions/46558006/how-to-set-the-actionbar-title-in-middle-instead-of-the-default-left-aligned-pos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!