3

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

在此处输入图像描述

4

3 回答 3

6

您应该能够通过setCustomView(). Roman Nurik 有一篇关于实现 DONE+DISCARD 的方法的 G+ 帖子,并提供了源代码。虽然他的代码没有使用 ActionBarSherlock,但我怀疑它会移植过来。

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

于 2012-10-18T13:28:32.523 回答
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>
于 2013-03-13T16:23:55.470 回答
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)

我将它用于 aFragment因此getSherlockActivity(). 如果在 中使用Activity,您可以自由省略该部分。

有两个地方可以设置这个自定义 ActionBar:

  1. 在 Activity onCreate()(或 Fragments onAttach())中,

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

关于如何将 ActionBar 恢复为“原始”表单的过程(在选择 DONE 或 DISCARD 之后),您可以参考这个答案

于 2013-09-03T14:15:31.637 回答