2013-10-08 27 views
14

您好我正在開發Android應用程序。在我的應用程序中,我使用操作欄。我想讓中心在行動欄中對齊我的窗口標題。我知道可以使用xml文件。但我想用風格去做,以便適用於我的每一個窗口,如何做到這一點需要幫助。謝謝。使用android中的樣式中心對齊操作欄中的標題。

+0

我試過用xml工作,但我想用樣式來做。因爲對於每一個窗口我的標題將在中間。所以我想用樣式來做。 – nilkash

+0

可以任何一個幫助http://stackoverflow.com/questions/31803463/how-to-set-action-bar-title-center-in-navigation-drawer – Aditya

回答

9

我知道這是可能使用XML文件。但我想用風格 ,使其適用於我的每一個窗口,如何做到這一點需要 幫助做到這一點。

仍然使用XML來做到這一點。只需將樣式應用程序廣泛應用於單個活動即可。請參閱Android文檔完整地描述:

https://developer.android.com/training/basics/actionbar/styling.html

然後套用您的主題,你的整個應用程序或個別活動:

<application android:theme="@style/CustomActionBarTheme" ... /> 

編輯:下面是你必須處理標題的中心位置:

在action_bar.xml中,您將擁有如下所示的內容:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:orientation="vertical"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:text="SOME TITLE" 
    android:textSize="18sp" /> 
</RelativeLayout> 

然後在您的onCreate(),你會碰到這樣的:

getSupportActionBar().setCustomView(R.layout.action_bar); 

如果你需要有這樣的每一次活動,你有2種選擇:

  1. 蠻力方式:將上面的代碼行放在每個Activity的onCreate中。
  2. 擴展活動,把你的動作條初始化代碼在OnCreate。然後爲每個活動擴展您的自定義活動類。只要不要忘記在每個Activity的onCreate()中調用super()。
+1

謝謝你的幫助我檢查了你給的鏈接。其實我可以設置標題的顏色大小,但不能做senter對齊。你對此有任何想法。需要幫忙。謝謝。 – nilkash

+0

更新了我的答案。希望它可以幫助你。 – SBerg413

+0

感謝您的幫助。我以同樣的方式做了。但仍然覺得我不允許通過樣式來對其進行中心化處理:( – nilkash

29

將ActionBarTitle居中而不使用自定義佈局的簡單方法。

但是,第一隱藏了圖標爲前:

myActionBar.setIcon(new ColorDrawable(Color.TRANSPARENT)); 

private void centerActionBarTitle() { 
    int titleId = 0; 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
     titleId = getResources().getIdentifier("action_bar_title", "id", "android"); 
    } else { 
     // This is the id is from your app's generated R class when 
     // ActionBarActivity is used for SupportActionBar 
     titleId = R.id.action_bar_title; 
    } 

    // Final check for non-zero invalid id 
    if (titleId > 0) { 
     TextView titleTextView = (TextView) findViewById(titleId); 
     DisplayMetrics metrics = getResources().getDisplayMetrics(); 

     // Fetch layout parameters of titleTextView 
     // (LinearLayout.LayoutParams : Info from HierarchyViewer) 
     LinearLayout.LayoutParams txvPars = (LayoutParams) titleTextView.getLayoutParams(); 
     txvPars.gravity = Gravity.CENTER_HORIZONTAL; 
     txvPars.width = metrics.widthPixels; 
     titleTextView.setLayoutParams(txvPars); 
     titleTextView.setGravity(Gravity.CENTER); 
    } 
} 
+0

This works .. !! –

+2

重複與action_bar_subtitle相同的操作欄也會中心對齊 – Intrications

+3

Works,但如果ActionBar有MenuItems,則標題會移動 – saiyancoder

4

Unfortunally,SBerg413的方式不適合我的工作。

我的自定義佈局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/custom_action_bar_title" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:text="test" 
     android:textColor="@color/action_bar_title_color" 
     android:textSize="@dimen/action_bar_title_size" /> 
</RelativeLayout> 

和所有完美的作品:
http://i.stack.imgur.com/MvQgj.png

UPD:這個活動我喜歡用我所有的活動家長,應該有操作欄。

public class ActionBarCustomizationActivity extends ActionBarActivity 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     this.getSupportActionBar().setDisplayShowCustomEnabled(true); 

     LayoutInflater inflater = LayoutInflater.from(this); 
     View v = inflater.inflate(R.layout.custom_action_bar, null); 

     TextView titleTextView = (TextView) v.findViewById(R.id.custom_action_bar_title); 
     titleTextView.setText(this.getTitle()); 
     titleTextView.setTypeface(App.getInstance().getActionBarTypeFace()); 

     this.getSupportActionBar().setCustomView(v); 
    } 
} 
3

@拿破崙的答案幫了我,但在我的情況下,我使用的是Xamarin android(monodroid)。這裏是他的代碼在C#中的翻譯:

var titleId = Resources.GetIdentifier("action_bar_title", "id", "android"); 
var titleTextView = FindViewById<TextView>(titleId); 
var layoutParams = (LinearLayout.LayoutParams) titleTextView.LayoutParameters; 
layoutParams.Gravity = GravityFlags.CenterHorizontal; 
layoutParams.Width = Resources.DisplayMetrics.WidthPixels; 
titleTextView.LayoutParameters = layoutParams; 
titleTextView.Gravity = GravityFlags.Center; 
//Added padding because it is slightly off centered. 
titleTextView.SetPadding(100, 0,0,0); 
+0

你從哪裏調用這段代碼?我已經從主要活動中完成了它,但是當OnConfigurationChange從旋轉中觸發時,重心會被重置。 – Ian