代码之家  ›  专栏  ›  技术社区  ›  OBX

如何将ActionBar标题设置在中间,而不是默认的左对齐位置?

  •  1
  • OBX  · 技术社区  · 7 年前

    正在尝试设置 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_布局。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>
    

    然而,这并没有产生预期的结果 ,这种方法有什么问题,如何解决?

    5 回复  |  直到 7 年前
        1
  •  5
  •   AskNilesh    7 年前

    尝试此使用自定义 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);
    
        2
  •  1
  •   akhilesh0707    7 年前

    简单方法使用 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
  •  0
  •   Kuls    7 年前

    你需要使用 Toolbar xml 文件,您可以使用 TextView 工具栏 工具栏 . 一旦处理完xml文件,就可以定义 进入 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>
    

    Toolbar toolbar;
    toolbar = (Toolbar) findViewById(R.id.ar_toolbar);
    toolbar.setTitle("");
    
        4
  •  0
  •   Nabin Bhandari    7 年前

    你不需要 Toolbar 为了这个。只需使用以下主题 Theme.AppCompat.Light.DarkActionBar 具有 ActionBar 并使用以下代码:

    @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
  •  0
  •   Nirmal Vaghasiya    7 年前
    <?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>