簡體   English   中英

Android 如何將頂部 actionBar 的標題居中?

[英]Android How to center title of top actionBar?

開始我的目標是將我的標題居中。 我通過選擇底部導航活動模板開始了一個項目。 這帶有 3 個片段和一個主要活動。 我試圖將頂部操作欄的標題居中。 我試圖通過 id 訪問 mobile_navigation 並在 MainActivity 中對齊文本,但這似乎不起作用。 我還嘗試在 homeFragment.xml 中添加一個 textAlignment 屬性,但這也不起作用。 我沒有找到任何類似的情況,有人知道我怎樣才能解決這個問題?

mobile_navigation.xml:

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mobile_navigation"
    app:startDestination="@+id/navigation_home">

    <fragment
        android:id="@+id/navigation_home"
        android:name="com.Example.exmaple.ui.home.HomeFragment"
        android:label="@string/app_name"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/navigation_dashboard"
        android:name="com.Example.exmaple.ui.dashboard.DashboardFragment"
        android:label="@string/title_dashboard"
        tools:layout="@layout/fragment_dashboard" />

    <fragment
        android:id="@+id/navigation_notifications"
        android:name="com.Example.exmaple.ui.notifications.NotificationsFragment"
        android:label="@string/title_notifications"
        tools:layout="@layout/fragment_notifications" />
</navigation>

我還嘗試添加:

android:gravity="center"

歡迎任何反饋。

要在Toolbar使用自定義標題,您需要做的就是記住Toolbar只是一個奇特的 ViewGroup,因此您可以像這樣添加自定義標題:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_top"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/action_bar_bkgnd"
    app:theme="@style/ToolBarTheme" >


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


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

這意味着您可以隨意設置TextView樣式,因為它只是一個普通的TextView 因此,在您的活動中,您可以像這樣訪問標題:

Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top);
TextView mTitle = (TextView) toolbarTop.findViewById(R.id.toolbar_title);

setSupportActionBar(toolbar);
mTitle.setText(toolbar.getTitle());

getSupportActionBar().setDisplayShowTitleEnabled(false);

試試下面的代碼可能對你有用

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

最好為工具欄添加一個布局文件並將標題居中,並在創建時動態添加或包含在您的片段布局文件中。

我能夠找出解決方案。 我會發布它以防萬一有人遇到這個問題。

我在 Main Activity xml 中添加了以下內容:

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolBar"
    android:elevation="5dp"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorBlack">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorTransparent"
        android:layout_alignParentStart="true"
        android:layout_gravity="start"
        android:text="@string/log_out"
        android:textAlignment="center"
        android:textColor="@color/lightblue"

        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:textColor="@color/colorApp"
        android:textSize="20sp"/>

        <ImageButton
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:src="@drawable/ic_add"
            android:tint="@color/lightblue"
            android:layout_alignParentEnd="true"
            android:layout_gravity="end"
            android:background="@color/colorBlack" />


</androidx.appcompat.widget.Toolbar>

我還從 Main Activity XML 中的 androidx.constraintlayout.widget.ConstraintLayout 開始標記中拿走了 padding top

在styles.xml 中添加了以下樣式:

<style name="AppTheme.NoActionBar" parent="AppTheme">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
</style>

最后,在 Android 清單中,我將以下主題添加到主活動中:

android:theme="@style/AppTheme.NoActionBar"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM