0

I have a layout like this :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp" >

    <TextView
        android:id="@+id/title_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_toRightOf="@+id/backToScan"
        android:gravity="center"
        android:textAllCaps="true"
        android:textAppearance="?android:attr/textAppearanceLarge"

        android:textStyle="bold" />

    <ImageView
        android:id="@+id/backToScan"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="8dp"
        android:src="@drawable/back_button" />

</RelativeLayout>

Now I added it successfuly as a custom view for action bar . But the problem is i cannot center the TextView , I have used a lot of way like android:gravity="center" , android:layout_centerHorizontal="true" , but it doesn't work.

It always float to the left. How can I center it?

enter image description here

xtiger
  • 1,446
  • 2
  • 15
  • 33

1 Answers1

1

The options menu is preventing the centering of the TextView. Add the following to your Activity:

@Override
public boolean onPrepareOptionsMenu (Menu menu) {
    return false;
}

Now it will work.

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
  • hi ZygoteInit, thanks for your answer, but I also want to use the options menu too , is there any other way? – xtiger Feb 20 '15 at 20:52
  • If you want to center the `TextView`, the only way to do that is by removing the options menu. What you could do is, shift the options menu items to the bottom bar (split action bar). This could work. – Yash Sampat Feb 20 '15 at 20:55