我有这样的活动:
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。
问题:
我想要什么:
我想显示一个带有自定义布局的ActionBar和一个没有任何菜单的homeAsUp按钮。 我怎么能这样做?
答案 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按钮。