【问题标题】:Android Done Cancel ActionbarAndroid 完成取消操作栏
【发布时间】:2012-10-18 12:05:13
【问题描述】:

有什么方法可以使用 actionbarsherlock 库来创建像下面这样的自定义操作栏

【问题讨论】:

    标签: android actionbarsherlock


    【解决方案1】:

    您应该可以通过setCustomView() 完成此操作。 Roman Nurik 有a G+ post on ways of implementing DONE+DISCARD,有source code available。虽然他的代码没有使用 ActionBarSherlock,但我怀疑它会移植过来。

    但是,请记住,Android 2.x 上的按钮背景看起来与 3.0+ 上的略有不同,因此您可能需要做更多的工作才能让操作栏空间中的按钮看起来像你想要的。

    【讨论】:

      【解决方案2】:

      这是您在 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>
      

      【讨论】:

      【解决方案3】:

      要完成使用说明@Vlasto Benny Lava has described,这里是实际设置 ActionBar 的代码(基于 Roman Nurik 的 API 14 的 original code)- 适用于 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. 在活动中onCreate()(或在片段中onAttach()),

      2. 在活动/片段中onCreateOptionsMenu()

      关于如何将ActionBar恢复为“原始”表单(选择DONE或DISCARD后),您可以参考this answer

      【讨论】: