2011-07-04 14 views
0
((TextView)((FrameLayout)((LinearLayout)((ViewGroup)getWindow().getDecorView()).getChildAt(0)).getChildAt(0)).getChildAt(0)).setGravity(Gravity.CENTER); 

我已經使用這個上面的代碼對準我的活動稱號center但它將給了我一個錯誤,即「FrameLayout不能被解析爲一個類型」,如果刪除FrameLayout然後它會給我同樣的錯誤LinearLayout。我還通過android.xml文件中加入這些行更改標題欄:如何在Android中心或任何位置對齊Activity的標題?

  <application android:label="@string/app_name"> 
      <activity android:name=".OptionsmenuAct" 
       android:label="@string/app_name" 

       android:theme="@android:style/Theme.Translucent"> 

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

現在我根據我的需要努力align,也想增加title text大小。

我怎樣才能做到這一點plz幫助我............

+0

更新的main.xml代碼 –

回答

1
((TextView)((LinearLayout)((ViewGroup) getWindow() 
    .getDecorView()).getChildAt(0)).getChildAt(0)).setGravity(Gravity.CEN‌​TER); 

這對我有效。 但是這是一個沒有記錄的方法。谷歌已經知道從根本上改變無證的數據結構,並且不能保證這種特定的技術將來會有效。所以我會建議ü使用它在try/catch塊,如:

try { 
((TextView)((LinearLayout)((ViewGroup) getWindow() 
    .getDecorView()).getChildAt(0)).getChildAt(0)).setGravity(Gravity.CENTER); 
     } catch (Exception e1) { 
      e1.printStackTrace(); 
     } 
1

我做一點顯然烏斯曼方法的變種:

((TextView)getWindow().getDecorView().findViewById(android.R.id.title)).setGravity(Gravity.CENTER); 
+0

getChildAt( 0)).getChildAt(0)在Android 5.1上不起作用,findViewById(android.R.id.title)的作品。另外,爲了安全起見,我建議檢查findViewById for null和wrap(TextView)類型轉換的結果try {} catch – hutorny