Center title of activity in android action bar

2019-02-19 21:35发布

问题:

Right now the title of my activity shows to the left as < Title and then the other menu item shows to the right. I want to center my title and leave out the <. How do I do that? I am using typical menu which I call using

public boolean onOptionsItemSelected(MenuItem item) 

and

@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;
}

EDIT:

I managed to get it to show using

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

where yesterday_title_bar is a relativeLayout

But it's showing half the height of the views. So I created the following style. But when I apply the style in the manifest. The manifest says it cannot find the resource.

<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>

Manifest:

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

回答1:

I also used actionbar sherlock and i set header position by this method. you can set a custom textview and can set its gravity to center. use the below method and tell me.

    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);
  }

and here is the layout i used:

  <?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" />



回答2:

The theme is not in android: style but in style. Change your manifest to this..

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

EDIT: Maybe it's your parent theme. What was your theme before? Try changing your parent theme like this:

    <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>


回答3:

Just use a custom view for your actionbar in your activity...

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.

The view could probably be something like this:

<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>