首先,您必须为自定义操作栏创建布局,以便根据您的要求定义功能。这是xml文件...
自定义操作栏的布局:
<ImageView
android:id="@+id/custom_actionbar_back_iv"
android:layout_width="@dimen/small_margin_ar"
android:layout_height="@dimen/very_small_margin_ar"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/up_navigation"/>
<TextView
android:id="@+id/custom_actionbar_titleText_tv"
style="@style/wrapscreen"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/custom_actionbar_back_iv"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/white"
android:textSize="@dimen/above_medium_text_size" />
<ImageButton
android:id="@+id/custom_actionbar_goToArchiveActivity_ib"
style="@style/wrapscreen"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="@dimen/medium_margin"
android:background="@null"
android:src="@drawable/ic_action_overflow" />
</RelativeLayout>
这里是实现..
在 onCreate() 方法中定义 setupactionBar() 方法
private void setUpActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
LayoutInflater layoutInflater = LayoutInflater.from(this);
View customActionBarView = layoutInflater.inflate(R.layout.custom_actionbar, null);
// this view is used to show tile
TextView ActivityTitleTV = (TextView) customActionBarView.findViewById(R.id.custom_actionbar_titleText_tv);
ActivityTitleTV.setText(R.string.title_text_archive);
//this view can be used for back navigation
ImageView backToRecorderIV = (ImageView) customActionBarView.findViewById(R.id.custom_actionbar_back_iv);
backToRecorderIV.setVisibility(View.VISIBLE);
backToRecorderIV.setOnClickListener(this);
//Another view which has up navigation
ImageButton goToArchiveActivityIB = (ImageButton) customActionBarView.findViewById(R.id.custom_actionbar_goToArchiveActivity_ib);
goToArchiveActivityIB.setVisibility(View.GONE);
actionBar.setCustomView(customActionBarView);
actionBar.setDisplayShowCustomEnabled(true);
}
//listener for back navigation
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.custom_actionbar_back_iv:
Intent recorder = new Intent(ArchiveActivity.this, RecorderActivity.class);
startActivity(recorder);
finish();
break;
}
}
希望这可以帮助...