将菜单项添加到自定义操作栏会替换文本

时间:2018-02-10 15:46:25

标签: android android-actionbar

我正在尝试为我的应用创建自定义操作栏。我希望应用程序名称显示在操作栏的中心,左侧我想要一个菜单​​项,而在右侧我想要一个自定义按钮。问题是当我添加菜单项时,应用程序名称从中心向右侧移动,好像有什么东西在那里推动它,我该如何解决这个问题,以便应用程序名称将始终保留在中心? 谢谢!

这是我的动作栏布局xml

<?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="wrap_content"
    android:background="@color/colorWhite">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/action_bar_text"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:textColor="@color/colorBlack"
        android:textStyle="bold"
        android:textSize="16sp"
        android:text="@string/app_name"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="@string/customButton"/>

</RelativeLayout>

这是我的菜单项xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.rashish.myapplication.MainActivity">

    <item
        android:id="@+id/back_action"
        android:orderInCategory="100"
        android:title="@string/back"
        app:showAsAction="always"
       />

    <item
        android:id="@+id/app_icon"
        android:orderInCategory="100"
        android:title=""
        android:icon="@drawable/logo"
        app:showAsAction="always"
        android:enabled="false"
        />

</menu>

这就是我设置自定义操作栏的方式:

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorWhite)));
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.action_bar_layout);

这是我添加菜单项的方式:

public boolean onCreateOptionsMenu(Menu menu) {
        this.mMenu = menu;
        getMenuInflater().inflate(R.menu.menu_main, mMenu);

        MenuItem itemBack = mMenu.findItem(R.id.back_action);
        MenuItem appIconItem = mMenu.findItem(R.id.app_icon);


        itemBack.setVisible(false);
        appIconItem.setVisible(true);


        return true;
    }
  

UPDTAE

使用Mike M.建议解决方案的新布局文件。按钮放置正确但是带有标题的文本仍然朝向按钮移动。

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="0dp"
    android:minHeight="?attr/actionBarSize"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    app:contentInsetStartWithNavigation="0dp"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <TextView
        android:id="@+id/action_bar_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorWhite"
        android:textSize="20sp"
        android:text="@string/app_name"
        android:layout_gravity="center"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|center_vertical"
        android:text="click me"/>

</android.support.v7.widget.Toolbar>
  

更新

这是我的结果截图。 enter image description here

这是我得到的结果,在我看来,App名称被推向按钮而不是居中,似乎工具栏布局不是全宽。

0 个答案:

没有答案
相关问题