2016-09-16 13 views
1

私はAndroidプログラミング時に新しく、すべての機能についてはわかりません。 私が理解したように、生成されたすべてのアクティビティには、app_name文字列リソースを含むタイトルバーがあります。私は唯一の活動のタイトルを変更することができたが、タイトルのテキストの配置を変更する方法を理解していない。私が意味する、the'reは次のように生成された:Androidのタイトルバーのテキストの中央揃え

enter image description here

が、私はそれがこのようなことする必要があります:あなたが持っているしたい場合(ABSの中央にタイトルを持っているために

enter image description here

答えて

2

サポートツールバーを使用している場合、その方法は簡単です。 ちょうどそれ内のテキストビューを追加し、それを中心に行います。あなたがそうのようなタイトルにアクセスすることができ、あなたの活動の

<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); 
0

これはデフォルトの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> 

今、あなただけのタイトルのアクションバーを持っている必要があります。カスタム背景を設定する場合は、上記のレイアウトで設定します(ただし、android:layout_height = "match_parent"を設定することを忘れないでください)。

関連する問題