【问题标题】:How to set the ActionBar title in middle instead of the default left aligned position?如何将 ActionBar 标题设置在中间而不是默认的左对齐位置?
【发布时间】:2018-03-15 10:37:18
【问题描述】:

尝试将ActionBar 标题设置在中间而不是默认的左对齐位置。为此,根据其他答案,这就是我所做的:

    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>

但是,这不会产生预期的结果(即,这会呈现文本但仍然左对齐),这种方法有什么问题以及如何解决这个问题?

【问题讨论】:

  • 你尝试添加getSupportActionBar().setDisplayShowCustomEnabled();在 getSupportActionBar().setCustomView(R.layout.actionbar_layout) 之前; ?

标签: java android xml android-actionbar


【解决方案1】:

试试这个使用自定义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>

现在在 java 文件中

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

【讨论】:

  • @Nilesh,感谢您的快速方法,但我不能使用自定义布局文件中的工具栏,还有其他元素依赖于 Activity 中的工具栏,即我确实有一个自定义 SmartTabLayout(github.com/ogaclejapan/SmartTabLayout) 与 Toolbar 一起使用,因此不能在操作栏的自定义布局文件中使用 Toolbar :/
  • 好的,我明白了! , 让我试试 :)
  • @OBX 如有任何疑问,请告诉我
  • 工作就像一个魅力 :) 再次感谢!
【解决方案2】:

简单的方法在您的TextView 上使用android:layout_gravity="center" 并在初始化您的TextView 并设置值之后使用下面的代码隐藏默认标题

在你的活动中:

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

自定义工具栏 XML 代码:

<?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】:

    您需要在您的xml 文件中使用Toolbar,您可以在Toolbar 中使用TextView。您可以将自定义视图添加到Toolbar。完成 xml 文件后,您可以将 Toolbar 定义为 Activity 类。

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

    在Activity类中你可以定义为

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

    【讨论】:

      【解决方案4】:

      您不需要Toolbar。只需在您的活动中使用Theme.AppCompat.Light.DarkActionBarActionBar 之类的主题并使用以下代码:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-02-15
          • 2013-08-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-19
          相关资源
          最近更新 更多