首页 > 解决方案 > 如何使 Android 标签居中

问题描述

一直试图将我的 android 标签居中。我应该为此使用操作栏吗?

    <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:exported="true"
    android:theme="@style/Theme.TurboMileage">
    <activity android:name=".MainActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

这是我的strings.xml

<resources>
<string name="app_name">Turbo Mileage</string>

标签: android

解决方案


您必须将主题与 NoActionBar 一起使用并创建工具栏:

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?actionBarSize"
    android:background="@color/colorPrimary"
    app:contentInsetEnd="0dp"
    app:contentInsetLeft="0dp"
    app:contentInsetRight="0dp"
    app:contentInsetStart="0dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_toEndOf="@+id/ivSetting"
            android:text="@string/app_name"
            android:textColor="@color/white"
            android:textSize="@dimen/font_normal" />

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/ivBack"
            android:layout_width="?actionBarSize"
            android:layout_height="match_parent"
            android:background="?android:selectableItemBackground"
            android:padding="@dimen/spacing_normal"
            app:srcCompat="@drawable/ic_back" />

    </RelativeLayout>
</androidx.appcompat.widget.Toolbar>

推荐阅读