How to align title at center of ActionBar in default theme(Theme.Holo.Light)

后端 未结 12 2067
野性不改
野性不改 2020-11-27 12:08

I searched a lot but can not find a way, How can I set then title at center of ActionBar instead of left aligned. I used below code to set the title at center :



        
相关标签:
12条回答
  • 2020-11-27 12:45

    Check out new Tool bar on support library class in Lollipop update you can design actionbar by adding toolbar in your layout

    add these items in your app theme

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

    Create your toolbar in a layout and include your textview in center design your toolbar

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

    add your action bar as tool bar

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

    please ensure that you need to include toolbar on your resource file like this

    <?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>
    
    0 讨论(0)
  • 2020-11-27 12:50

    I think by positioning views on action bar you will reach what you want.On action bar's layout when layout params set to match parent it does not match full width that's why I did it programmatically where I succeed.By setting width (it works like layout_weight="value") we can set our view wherever we want:

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

    and here is getWidth() method:

     public int getWidth() { 
        DisplayMetrics dm = new DisplayMetrics();
         mActivity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels; 
       }
    
    0 讨论(0)
  • 2020-11-27 12:51

    I know my answer is not on time but this is purely code no xml required.
    This is for use in 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);
    }
    


    This is for use in 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)
  • 2020-11-27 12:53

    I had the same problem, and because of the "Home" button added automatically in the toolbar, my text was not exactly entered.

    I fixed it the dirty way but it works well in my case. I simply added a margin to the right of my TextView to compensate for the home button on the left. Here's my toolbar layout :

    <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>
    
    0 讨论(0)
  • 2020-11-27 12:54

    You can force the toolbar by wrapping title and level right padding which has default left padding for title. Than put background color to the parent of toolbar and that way part which is cut out by wrapping title is in the same color(white in my example):

    <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>
    
    0 讨论(0)
  • 2020-11-27 12:56

    It seems there is no way to do this without custom view. You can get the title view:

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

    But changing of gravity or layout_gravity doesn't have an effect. The problem in the ActionBarView, which layout its children by itself so changing of layout params of its children also doesn't have an effect. To see this excecute following code:

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