如何在Android Studio中居中应用顶部的标题文本?
Hey there! I see you're trying to center the "Login to Notely" title in your app's top action bar, and you’ve set android:gravity="center" in your activity tag—unfortunately, that property controls the alignment of the activity’s content, not the action bar title itself. Let’s fix this with a few straightforward methods:
Method 1: Set Title Alignment via Activity Code
This is the quickest approach if you only need the title centered for this specific activity.
Kotlin Code
Add this to your ActivityLogin.kt file inside the onCreate method:
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_login) // Center the action bar title supportActionBar?.apply { setDisplayOptions(ActionBar.DISPLAY_SHOW_CENTERED) } }
Java Code
If you’re using Java, add this to ActivityLogin.java:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { // Center the action bar title actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CENTERED); } }
Method 2: Center Title Using Theme Styles
Use this if you want the centered title style to apply across multiple activities, or prefer styling via XML.
- Open your
res/values/styles.xmlfile and add these styles:
<!-- Custom action bar style with centered title --> <style name="CenteredActionBar" parent="@style/Widget.MaterialComponents.ActionBar.Solid"> <item name="titleTextStyle">@style/CenteredTitleText</item> </style> <!-- Style for centered title text --> <style name="CenteredTitleText" parent="@style/TextAppearance.MaterialComponents.Headline6"> <item name="android:gravity">center</item> </style>
- Update your app’s theme (or create a custom theme) to use this action bar style:
<style name="AppTheme.CenteredTitle" parent="Theme.MaterialComponents.Light.DarkActionBar"> <!-- Keep your existing theme properties, then add this --> <item name="actionBarStyle">@style/CenteredActionBar</item> </style>
- Apply this theme to your
ActivityLogininAndroidManifest.xml:
<activity android:layout_width="match_parent" android:layout_height="match_parent" android:label="Login to Notely" android:name=".ActivityLogin" android:theme="@style/AppTheme.CenteredTitle"/>
Method 3: Use a Custom Toolbar (Recommended for Modern Apps)
If you’re using a custom Toolbar (which gives you more control over the action bar), follow these steps:
- Add a
Toolbarto your activity’s layout file (e.g.,activity_login.xml):
<androidx.appcompat.widget.Toolbar android:id="@+id/login_toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:title="Login to Notely" android:gravity="center" app:titleTextColor="@android:color/white"/>
- Set this
Toolbaras your activity’s action bar in code:
Kotlin
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_login) val toolbar = findViewById<Toolbar>(R.id.login_toolbar) setSupportActionBar(toolbar) }
Java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); Toolbar toolbar = findViewById(R.id.login_toolbar); setSupportActionBar(toolbar); }
All these methods will successfully center your "Login to Notely" title. Pick the one that best fits your app’s structure!
内容的提问来源于stack exchange,提问作者user13750693

