Android Head Center Position Title

I work with android (4.0 or higher). I want to use the action bar, and the name of the action bar is next to the icon. I want the title to be central and bold, as in the picture enter image description here

+3
source share
2 answers

You have to make your own layout for ActionBar.

In Java:

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

In XML (this will be R.layout.actionbar):

<?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:text="YOUR ACTIVITY TITLE"
    android:textStyle="bold"
    android:textColor="#ffffff"
    android:textSize="24sp" />
</LinearLayout>

Then you can add more things to the XML file if you want.

+1
source

You need to customize the action bar with a custom layout. how to change your code according to your needs

action = getActionBar();

        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL
                        | Gravity.CENTER_VERTICAL);
        View v = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                .inflate(R.layout.customlayout, null);

action.setCustomView(v, lp);

        action.setDisplayShowCustomEnabled(true);
        action.setDisplayHomeAsUpEnabled(true);
        action.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

note : you need to create your own layout: R.layout.customlayout.

+3

All Articles