2013-07-11 54 views
2

現在我的活動標題顯示在左側,爲< Title,然後其他菜單項顯示在右側。我想以我的標題爲中心並省略<。我怎麼做?我使用我稱之爲使用安卓動作欄中的活動中心標題

public boolean onOptionsItemSelected(MenuItem item) 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.yesterday, menu); 
    return true; 
} 

典型的菜單編輯:

我設法得到它使用

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
    setContentView(R.layout.activity_yesterday); 
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.yesterday_title_bar); 

其中yesterday_title_bar是一個RelativeLayout的顯示

但它顯示了視圖高度的一半。所以我創造了以下風格。但是當我在清單中應用風格時。清單說它找不到資源。

<style name="CustomWindowTitleBackground"> 
     <item name="android:background">#323331</item> 
    </style> 

    <style name="CustomTheme" parent="android:Theme"> 
     <item name="android:windowTitleSize">65dip</item> 
     <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item> 
    </style> 

清單:

<activity 
     android:name="com.company.YesterdayActivity" 
     android:theme="@android:style/CustomTheme" > 
    </activity> 
+0

查找到充氣的'ActionBar'自定義佈局。 – adneal

+0

我試過了。它說不能將自定義與其他功能混合使用。 –

+0

'android.util.AndroidRuntimeException:您不能將自定義標題與其他標題功能組合'我如何關閉它? –

回答

0

的主題是不是在安卓風格,但風格。您的清單更改爲這個..

<activity 
     android:name="com.company.YesterdayActivity" 
     android:theme="@style/CustomTheme" > 
    </activity> 

編輯: 也許這是你的母題。你以前的主題是什麼?試着改變你的母題是這樣的:

<style name="CustomWindowTitleBackground"> 
     <item name="android:background">#323331</item> 
    </style> 

    <style name="CustomTheme" parent="@android:style/Theme.Holo.Light"> 
     <item name="android:windowTitleSize">65dip</item> 
     <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item> 
    </style> 
+0

你知道如何讓主題影響一切在那個XML /活動?我現在的所有孩子的觀點都不一樣。例如我的EditText更高。 –

+0

自定義主題會扭曲佈局的其餘部分。如果我將它從清單移動到特定的自定義佈局,它甚至不會生效。 –

+0

看看我的編輯,讓我知道如何去。 – nedaRM

0

就在你的活動中使用自定義視圖您的動作條...

ActionBar actionBar = getSupportActionBar(); 
actionBar.setCustomView(R.layout.actionbar_layout); 
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM); 
actionBar.setDisplayHomeAsUpEnabled(false); // Remove '<' next to home icon. 

的觀點很可能是這樣的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/LinearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center_horizontal" 
    android:orientation="vertical" 
    tools:context=".MainActivity" > 

    <TextView 
     android:id="@+id/textViewActionBarTitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Your Title" /> 

</LinearLayout> 
+0

'actionBar.setTitle(」「);'給空指針異常 –

+0

那奇。在實例化actionBar之後? – Tonithy

+0

是的。注:我不得不使用'getActionBar()',而不是'getSupportActionBar()' –

2

我還使用了actionbar sherlock,並通過這種方法設置了標題位置。你可以設置一個自定義的文本視圖,並可以將其重心設置爲中心。使用下面的方法並告訴我。

private void setHeader(){ 
    LayoutInflater linflater = (LayoutInflater)MainAccountActivity.context.getSystemService(MainAccountActivity.context.LAYOUT_INFLATER_SERVICE); 
    View GalleryTitleView = linflater.inflate(R.layout.layout_header, null); 

    galleryTitleTv = (TextView) GalleryTitleView 
      .findViewById(R.id.GalleryTitleTv); 
    galleryTitleTv.setText(ITWAppUIConstants.TAB_AGENDA); 

    ActionBar.LayoutParams lp = new ActionBar.LayoutParams(
      ActionBar.LayoutParams.FILL_PARENT, 
      ActionBar.LayoutParams.WRAP_CONTENT); 
    lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL; 
    GalleryTitleView.setLayoutParams(lp); 
    getSupportActionBar().setCustomView(GalleryTitleView); 
    } 

,這裏是我使用的佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:background="@color/transparent" 
android:gravity="center" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/GalleryTitleTv" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:paddingLeft="10dip" 
    android:textColor="@color/white" 
    android:textSize="20dip" 
    android:textStyle="bold" />