Center title of activity in android action bar

前端 未结 3 1070
独厮守ぢ
独厮守ぢ 2021-01-15 09:56

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

相关标签:
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>
    
    0 讨论(0)
  • 2021-01-15 10:52

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

    0 讨论(0)
  • 2021-01-15 10:59

    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>
    
    0 讨论(0)
提交回复
热议问题