自定义操作栏添加了OnCreateOptionsMenu并不是中心标题Android?

时间:2014-10-31 05:26:13

标签: android android-layout

我创建了自定义操作栏,其中包含ImageView(左侧)和TextView(右侧)。但是我在活动中有不同的片段,我需要在其中显示onCreateOptionsMenu。如果有onCreateOptionsMenu操作栏的标题被推到左侧,并且不会查看中心。

如下面的操作栏+ onCreateOptionsMenu:

enter image description here

如您所见,当添加onCreateOptions菜单时,下载文本会向左侧倾斜。

如果没有onCreateOptions文本居中,如下所示:

enter image description here

这是我的自定义操作条形码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/menubutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:paddingLeft="5dp"
        android:src="@drawable/navigation_icon" />

    <TextView
        android:id="@+id/titleofscreen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textColor="#22c064"
        android:textSize="22sp"
        android:textStyle="bold" />

</RelativeLayout>

我不会在自定义操作栏中将onCreateOptionsMenu作为imageView放置,因为它会使事情复杂化。

1 个答案:

答案 0 :(得分:1)

试试这个。

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/menubutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:paddingLeft="5dp"
            android:src="@drawable/navigation_icon" />

        <TextView
            android:id="@+id/titleofscreen"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/menubutton"
            android:gravity="center"
            android:textColor="#22c064"
            android:textSize="22sp"
            android:textStyle="bold" />

    </RelativeLayout>