有没有办法使用actionbarsherlock库创建如下屏幕所示的自定义操作栏?
setCustomView()
来实现此目标。Roman Nurik在一篇G+帖子中介绍了实现DONE+DISCARD的方法, 并提供了源代码。虽然他的代码没有使用ActionBarSherlock,但我认为它也可以移植过来。以下是在xml中实际执行的方法
在Honeycomb及以上版本中使用Roman的布局
/layout-v11/actionbar_custom_view_done_discard.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="?android:attr/dividerVertical"
android:dividerPadding="12dp"
android:orientation="horizontal"
android:showDividers="middle" >
<include layout="@layout/actionbar_discard_button" />
<include layout="@layout/actionbar_done_button" />
</LinearLayout>
/layout-v11/actionbar_discard_button.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/actionbar_discard"
style="?android:actionButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/selectable_background_mystyle" >
<TextView style="?android:actionBarTabTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingRight="20dp"
android:drawableLeft="@drawable/ic_menu_cancel"
android:drawablePadding="8dp"
android:gravity="center_vertical"
android:text="@string/menu_cancel" />
</FrameLayout>
/layout-v11/actionbar_done_button.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/actionbar_done"
style="?android:actionButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/selectable_background_mystyle" >
<TextView
style="?android:actionBarTabTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableLeft="@drawable/ic_menu_save"
android:drawablePadding="8dp"
android:gravity="center_vertical"
android:paddingRight="20dp"
android:text="@string/menu_save" />
</FrameLayout>
并将其与ActionBar Sherlock进行回溯移植
/layout/actionbar_custom_view_done_discard.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<include layout="@layout/actionbar_discard_button" />
<View
android:layout_width="0.5dp"
android:layout_height="match_parent"
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:background="#55FFFFFF" />
<include layout="@layout/actionbar_done_button" />
</LinearLayout>
/layout/actionbar_discard_button.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/actionbar_discard"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingRight="20dp"
android:background="@drawable/selectable_background_mystyle" >
<TextView
style="@style/Widget.Sherlock.ActionBar.TabText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableLeft="@drawable/ic_menu_cancel"
android:drawablePadding="8dp"
android:gravity="center_vertical"
android:text="@string/menu_cancel" />
</FrameLayout>
/layout/actionbar_done_button.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/actionbar_done"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingRight="20dp"
android:background="@drawable/selectable_background_mystyle" >
<TextView
style="@style/Widget.Sherlock.ActionBar.TabText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableLeft="@drawable/ic_menu_save"
android:drawablePadding="8dp"
android:gravity="center_vertical"
android:text="@string/menu_save" />
</FrameLayout>
// BEGIN_INCLUDE (inflate_set_custom_view)
// Inflate a "Done/Cancel" custom action bar view.
final LayoutInflater inflater = (LayoutInflater) getSherlockActivity().getSupportActionBar()
.getThemedContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
final View customActionBarView = inflater.inflate(R.layout.actionbar_custom_view_done_discard,
null);
customActionBarView.findViewById(R.id.actionbar_done).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// "Done"
Toast.makeText(getActivity(), "DONE", Toast.LENGTH_SHORT).show();
}
});
customActionBarView.findViewById(R.id.actionbar_discard).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// "Cancel"
Toast.makeText(getActivity(), "DISCARD", Toast.LENGTH_SHORT).show();
}
});
// Show the custom action bar view and hide the normal Home icon and title.
final ActionBar bar = getSherlockActivity().getSupportActionBar();
bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM
| ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
bar.setCustomView(customActionBarView, new ActionBar.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
// END_INCLUDE (inflate_set_custom_view)