安卓完成取消操作栏

3
有没有办法使用actionbarsherlock库创建如下屏幕所示的自定义操作栏?
3个回答

6
你可以通过使用setCustomView()来实现此目标。Roman Nurik在一篇G+帖子中介绍了实现DONE+DISCARD的方法, 并提供了源代码。虽然他的代码没有使用ActionBarSherlock,但我认为它也可以移植过来。
然而,请注意,在Android 2.x上,按钮背景看起来与3.0+上有些不同,因此您可能需要做更多的工作来使您的按钮在操作栏空间中看起来符合您的要求。

4

以下是在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>

我已经实现并将TextView更改为Button。现在我不知道在哪里定义按钮的onClick方法。 - Dinesh T A
嗯,一切都是一样的,就像这个例子所示:https://code.google.com/p/romannurik-code/source/browse/misc/donediscard - urSus

1
为了补充 @Vlasto Benny Lava 所描述的 使用说明,这里是实际设置 ActionBar 的 代码(基于 Roman Nurik 的 API 14 原始代码)-- 适用于 ActionBarSherlock
// 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)

我在一个Fragment中使用它,因此需要用到getSherlockActivity()。如果在Activity中使用,则可以自由省略那部分。
有两个地方可以设置这个自定义ActionBar:
1. 在Activity的onCreate()(或Fragments的onAttach())中, 2. 在Activity/Fragments的onCreateOptionsMenu()中。
关于如何将ActionBar恢复为“原始”形式(在选择DONE或DISCARD后),您可以参考this answer中的步骤。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接