2013-08-24 20 views
38

我搜索了很多,但找不到方法,我如何設置標題在ActionBar的中心而不是左對齊。我用下面的代碼來設置標題爲中心:如何在默認主題(Theme.Holo.Light)中對齊ActionBar的標題

ViewGroup decorView= (ViewGroup) this.getWindow().getDecorView(); 
LinearLayout root= (LinearLayout) decorView.getChildAt(0); 
FrameLayout titleContainer= (FrameLayout) root.getChildAt(0); 
TextView title= (TextView) titleContainer.getChildAt(0); 
title.setGravity(Gravity.CENTER); 

但它給錯誤如下:

ClassCastException : com.android.internal.widget.ActionBarView can not 
be cast to android.widget.TextView. 

任何其他解決辦法..Any幫助將不勝感激。

+0

你試圖在Android上模仿iOS設計?因爲你真的不應該:) – REJH

回答

111

您可以創建自定義佈局並將其應用於actionBar。

這樣做,請按照這2個簡單的步驟:

  1. Java代碼的

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

哪裏R.layout.actionbar如下佈局。

  • 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:layout_gravity="center" 
        android:orientation="vertical"> 
    
    <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center" 
        android:id="@+id/action_bar_title" 
        android:text="YOUR ACTIVITY TITLE" 
        android:textColor="#ffffff" 
        android:textSize="24sp" /> 
    </LinearLayout> 
    
  • 它可以是作爲要複雜。試試看!

    編輯:

    要設置background您可以使用容器佈局屬性android:background(的LinearLayout在這種情況下)。您可能需要將佈局高度android:layout_height設置爲match_parent而不是wrap_content

    此外,您還可以添加一個LOGO/ICON它。爲此,只需在佈局中添加一個ImageView,並將佈局方向屬性android:orientation設置爲水平(或簡單地使用RelativeLayout並由您自己管理)。

    動態地改變上述的自定義操作欄的標題,這樣做:

    TextView title=(TextView)findViewById(getResources().getIdentifier("action_bar_title", "id", getPackageName())); 
    title.setText("Your Text Here"); 
    
    +0

    它的作品完美,但它使用自定義顯示..而我使用Theme.Holo.Light..Sight所有帶圖像,背景顏色和Backbutton的標識都被刪除,只有簡單的白色標題欄顯示在ActionBar的中心。是否有任何方式顯示它在我當前主題的中心?謝謝。 – Ponting

    +0

    沒有你的第二個答案沒有工作..它說FrameLayout無法解析鍵入。 – Ponting

    +0

    首先呢? –

    7

    似乎有沒有辦法做到這一點沒有自定義視圖。你可以拿到冠軍的看法:

    View decor = getWindow().getDecorView(); 
    TextView title = (TextView) decor.findViewById(getResources().getIdentifier("action_bar_title", "id", "android")); 
    

    gravitylayout_gravity改變沒有效果。 ActionBarView中的問題,它自己佈置子元素,所以改變其子元素的佈局參數也沒有效果。 要查看此excecute下面的代碼:

    ViewGroup actionBar = (ViewGroup) decor.findViewById(getResources().getIdentifier("action_bar", "id", "android")); 
    View v = actionBar.getChildAt(0); 
    ActionBar.LayoutParams p = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); 
    p.gravity= Gravity.CENTER; 
    v.setLayoutParams(p); 
    v.setBackgroundColor(Color.BLACK); 
    
    +0

    空指針異常的actionBar Apptheme是DarkActionBar – Karoly

    1

    我想通過定位操作欄上的意見,你會找到你想要的東西。在佈局參數設置爲匹配父項時,在操作欄的佈局上,它不匹配全寬 這就是爲什麼我通過編程實現了我成功的原因。通過設置寬度(它的工作方式類似於layout_weight =「value」),我們可以在任何我們想要的位置設置視圖:

    actionBar = getSupportActionBar(); 
        actionBar.setDisplayShowCustomEnabled(true); 
    
        LayoutInflater ll = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        LinearLayout linearLayout = (LinearLayout) ll.inflate(R.layout.custom_actionbar, null); 
        //inner layout within above linear layout 
        LinearLayout inner = (LinearLayout) linearLayout.findViewById(R.id.inner_linear_ractionbar); 
        inner.setMinimumWidth(getWidth() * 2/10); 
        // we will set our textview to center and display there some text 
        TextView t = (TextView) linearLayout.findViewById(R.id.center_text); 
        t.setWidth(getWidth() * 6/10); 
    
    
        Button b = (Button) linearLayout.findViewById(R.id.edit_button); 
        b.setWidth(getWidth() *3/ 20); 
    
        ImageButton imageButton = (ImageButton) linearLayout.findViewById(R.id.action_bar_delete_item); 
        imageButton.setMinimumWidth(deviceUtils.getWidth()/20); 
    

    ,這裏是的getWidth()方法:在棒棒糖支持庫類

    public int getWidth() { 
        DisplayMetrics dm = new DisplayMetrics(); 
        mActivity.getWindowManager().getDefaultDisplay().getMetrics(dm); 
        return dm.widthPixels; 
        } 
    
    6

    退房新的工具欄更新,你可以在你的佈局中添加工具欄

    添加這些設計動作條y中的項目我們的應用程序的主題

    <item name="android:windowNoTitle">true</item> 
        <item name="windowActionBar">false</item> 
    

    在佈局中創建工具欄,包括在中心的TextView的設計工具欄

    <android.support.v7.widget.Toolbar 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/toolbar" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="@color/acbarcolor"> 
         <RelativeLayout 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" > 
    
        <TextView 
         android:id="@+id/toolbar_title" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_centerHorizontal="true" 
         android:layout_centerVertical="true" 
         android:text="@string/app_name" 
         android:textColor="#ffffff" 
         android:textStyle="bold" /> 
    
    
    
        </RelativeLayout> 
    
    </android.support.v7.widget.Toolbar> 
    

    添加你的動作條爲工具欄

    toolbar = (Toolbar) findViewById(R.id.toolbar); 
         if (toolbar != null) { 
          setSupportActionBar(toolbar); 
          getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
         } 
    

    請確保您需要在您的資源文件中包含如下工具欄

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout 
        android:orientation="vertical" 
        android:layout_height="match_parent" 
        android:layout_width="match_parent" 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:tools="http://schemas.android.com/tools"> 
    
         <include 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          layout="@layout/toolbar" /> 
    
        <android.support.v4.widget.DrawerLayout 
    
        xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:tools="http://schemas.android.com/tools" 
        android:id="@+id/drawer_layout" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 
    
        <!-- Framelayout to display Fragments --> 
    
    
    
        <FrameLayout 
         android:id="@+id/frame_container" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent"> 
         <include 
    
          android:layout_width="match_parent" 
          android:layout_height="match_parent" 
          layout="@layout/homepageinc" /> 
    
    
    
         </FrameLayout> 
         <fragment 
          android:id="@+id/fragment1" 
          android:layout_gravity="start" 
          android:name="com.shouldeye.homepages.HomeFragment" 
          android:layout_width="250dp" 
          android:layout_height="match_parent" /> 
        </android.support.v4.widget.DrawerLayout> 
    
    </LinearLayout> 
    
    1

    Java代碼: 中的onCreate寫這篇文章()
    getSupportActionBar().setDisplayShowCustomEnabled(true); getSupportActionBar().setCustomView(R.layout.action_bar);

    ,併爲您的自定義視圖,只需使用FrameLayout裏,東peasy!
    android.support.v7.widget.Toolbar是另一種選擇

    <?xml version="1.0" encoding="utf-8"?> 
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 
    
        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="left|center_vertical" 
         android:textAppearance="?android:attr/textAppearanceLarge" 
         android:text="@string/app_name" 
         android:textColor="@color/black" 
         android:id="@+id/textView" /> 
    </FrameLayout> 
    
    2

    這其實工作原理:

    getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
    getActionBar().setCustomView(R.layout.custom_actionbar); 
        ActionBar.LayoutParams p = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); 
         p.gravity = Gravity.CENTER; 
    

    你必須定義custom_actionbar.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="50dp" 
        android:background="#2e2e2e" 
        android:orientation="vertical" 
        android:gravity="center" 
        android:layout_gravity="center"> 
    
        <ImageView 
         android:id="@+id/imageView1" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:src="@drawable/top_banner" 
         android:layout_gravity="center" 
         /> 
    </LinearLayout> 
    
    0

    我有同樣的問題,並且由於工具欄中自動添加了「主頁」按鈕,我的文本沒有完全輸入。

    我修正了它的骯髒的方式,但在我的情況下效果很好。我只是在我的TextView的右側添加了一個邊距來補償左側的主頁按鈕。這裏是我的工具欄佈局:

    <android.support.v7.widget.Toolbar 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:app="http://schemas.android.com/apk/res-auto" 
        android:elevation="1dp" 
        android:id="@+id/toolbar" 
        android:layout_width="match_parent" 
        android:layout_height="?attr/actionBarSize" 
        app:layout_collapseMode="pin" 
        android:gravity="center" 
        android:background="@color/mainBackgroundColor" 
        android:fitsSystemWindows="true" > 
    
        <com.lunabee.common.utils.LunabeeShadowTextView 
         android:id="@+id/title" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginRight="?attr/actionBarSize" 
         android:gravity="center" 
         style="@style/navigation.toolbar.title" /> 
    
    </android.support.v7.widget.Toolbar> 
    
    2

    我知道我的答案不準時,但這是純粹的代碼沒有xml必需的。
    這是用於Activity

    public void setTitle(String title){ 
        getSupportActionBar().setHomeButtonEnabled(true); 
        getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
        TextView textView = new TextView(this); 
        textView.setText(title); 
        textView.setTextSize(20); 
        textView.setTypeface(null, Typeface.BOLD); 
        textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
        textView.setGravity(Gravity.CENTER); 
        textView.setTextColor(getResources().getColor(R.color.white)); 
        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
        getSupportActionBar().setCustomView(textView); 
    } 
    


    這是用於Fragment

    public void setTitle(String title){ 
        ((AppCompatActivity)getActivity()).getSupportActionBar().setHomeButtonEnabled(true); 
        ((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
        TextView textView = new TextView(getActivity()); 
        textView.setText(title); 
        textView.setTextSize(20); 
        textView.setTypeface(null, Typeface.BOLD); 
        textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
        textView.setGravity(Gravity.CENTER); 
        textView.setTextColor(getResources().getColor(R.color.white)); 
        ((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
        ((AppCompatActivity)getActivity()).getSupportActionBar().setCustomView(textView); 
    } 
    
    +0

    好的和簡單的答案謝謝:) –

    0

    您可以通過包裝,其具有標題默認的左填充標題和水平右填充強制工具欄。比把工具欄的父母的背景顏色,這種方式是通過包裝標題切出的部分是在相同的顏色(在我的例子中是白色):

    <android.support.design.widget.AppBarLayout 
        android:id="@+id/appbar_layout" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="@color/white"> 
    
         <android.support.v7.widget.Toolbar 
          android:id="@+id/toolbar" 
          android:layout_width="wrap_content" 
          android:layout_height="56dp" 
          android:layout_gravity="center_horizontal" 
          android:paddingEnd="15dp" 
          android:paddingRight="15dp" 
          android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 
          app:titleTextColor="@color/black"/> 
    
    </android.support.design.widget.AppBarLayout>