簡體   English   中英

Android中標題欄文本居中對齊

[英]Center align of text of title bar in Android

我是 Android 編程新手,所以我不知道所有功能。 據我了解,所有生成的活動都有一個標題欄,其中包含 app_name 字符串資源。 我設法更改了唯一活動的標題,但我不明白如何更改標題文本的對齊方式。 我的意思是,它們是這樣生成的:

在此處輸入圖片說明

但我需要它是這樣的:

在此處輸入圖片說明

如果您使用的是支持工具欄,則該方法將很容易。 只需在其中添加一個文本視圖並使其居中:

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

在您的活動中,您可以像這樣訪問標題:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_top);
TextView Title = (TextView) toolbar.findViewById(R.id.toolbar_title);

要在 ABS 中有一個居中的標題(如果你想在默認的 ActionBar 中有這個,只需刪除方法名稱中的“支持”),你可以這樣做:

在您的活動中,在您的onCreate()方法中:

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

xml文件

<?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"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="my Title"
        android:textColor="#ffffff"
        android:id="@+id/mytext"
        android:textSize="18sp" />

</LinearLayout>

現在你應該有一個只有標題的 Actionbar。 如果要設置自定義背景,請在上面的 Layout 中進行設置(但不要忘記設置 android:layout_height="match_parent")。

暫無
暫無

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

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