未设置菜单时,我的ActionBar中没有HomeAsUp按钮

时间:2014-09-30 13:18:39

标签: android android-activity android-actionbar

我有这样的活动:

public class MapActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);

    ActionBar actionBar = getSupportActionBar();
    actionBar.setCustomView(R.layout.action_bar_layout);
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME
            | ActionBar.DISPLAY_HOME_AS_UP);
    actionBar.setDisplayHomeAsUpEnabled(true);
}

}

布局action_bar_layout.xml在中心只包含两个TextView。

问题:

  1. 在ActionBar中没有任何按钮返回上一个活动。
  2. 如果我从像R.menu.example这样的资源设置菜单,则会出现homeAsUp按钮。
  3. 如果我设置一个空菜单(没有项目的菜单),则不会显示homeAsUp。
  4. 我想要什么

    我想显示一个带有自定义布局的ActionBar和一个没有任何菜单的homeAsUp按钮。 我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

Activity的{​​{1}}

中使用此功能
onCreate()

创建一个新的xml custom_actionbar.xml

ActionBar actionBar = getSupportActionBar();
actionBar.setCustomView(R.layout.custom_actionbar);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM|ActionBar.DISPLAY_SHOW_HOME        |ActionBar.DISPLAY_HOME_AS_UP);
actionBar.setDisplayHomeAsUpEnabled(true);

在您的活动中删除所有过度使用的方法,例如onCreateOptionsMenu()和<?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="match_parent" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="custom" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android`enter code here`:layout_height="wrap_content" android:text="custom" /> </LinearLayout> 。我已经执行了这段代码。它显示的操作栏只有两个文本视图,没有菜单选项,启用了homeAsUp按钮。