2

I use this method to align action bar title to center

How to make android action bar title center aligned?

But my action bar has menu, and then action bar width is not same to screen

so the title is not exactly center.

Is there other way to center the title?

enter image description here


Code:

actionbar.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="YOUR ACTIVITY TITLE"
        android:textColor="#000"
        android:textSize="24sp" />
</LinearLayout>


the activity which used custom action bar

public class WriteText extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_write_text);

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.write_tag, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
Community
  • 1
  • 1
CL So
  • 3,647
  • 10
  • 51
  • 95

1 Answers1

0

Maybe you can change the actionbar xml file to make one your own style of actionbar.

Gary Wang
  • 1
  • 2