2012-05-29 22 views
1

因此,我正在嘗試使用第一個使用第一個activity.java類的應用程序標題居中。使用活動在Android中將標題欄居中使用

這裏是我的代碼

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    -----setTitle(R.string.app_name1); //Sets the title for this activity using the app_name1 string 
    setContentView(R.layout.main); 

所以與我的標題(只是在這個問題上當然也沒有我真正的代碼)的「-----」是我的代碼行。我正在使用字符串app_name來從標題中提取標題,以便我能夠將標題居中?謝謝!

回答

0

對於在中心,您可以創建自定義標題欄居中標題欄中顯示爲:

 setContentView(R.layout.main); 
     getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, 
R.layout.title_bar); 

title_bar.xml:

<?xml version="1.0" encoding="utf-8" ?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/linear" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    > 
    <TextView 
     android:id="@+id/centertxt" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="title bar" 
     android:layout_centerInParent="true" 
     android:layout_marginLeft="5dip" 
     android:textStyle="bold" 
     android:textColor="#ffffff" 
     android:textSize="20sp" 
     /> 
    </RelativeLayout > 
+0

我不明白這是什麼一樣getWindow()setFeatureInt (Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);你是在說使用其中一個還是另一個?無論是title_bar.xml還是我剛纔指定的那個?如果你能解釋更多,這真的很棒。 – Fernando

+0

不,我不是說使用多一個標題欄。我的意思是使用自定義標題欄,而不是系統默認標題欄。當您創建自定義標題欄時,需要使用xml佈局作爲第二個參數。在此佈局中,您可以根據需要在中心,右側或左側對齊文本... –

+0

for title_bar.xml在您的res中添加新的xml文件/佈局文件夾,並把你的XML標題bar.i你有任何問題,然後問我 –