Android完成取消操作栏
是否有任何方法可以使用actionbarsherlock库创建屏幕下方的自定义actionbarAndroid完成取消操作栏,android,actionbarsherlock,Android,Actionbarsherlock,是否有任何方法可以使用actionbarsherlock库创建屏幕下方的自定义actionbar 您应该能够通过setCustomView()实现这一点。罗曼·努里克有,有。虽然他的代码没有使用ActionBarSherlock,但我怀疑它会移植过来 但是,请记住,Android 2.x上的按钮背景与3.0+上的按钮背景看起来有点不同,因此您可能需要做更多的工作,以使操作栏空间中的按钮看起来符合您的需要。以下是xml中的实际操作方式 使用Roman的蜂窝式及以上布局 /layout-v11/ac
您应该能够通过
setCustomView()
实现这一点。罗曼·努里克有,有。虽然他的代码没有使用ActionBarSherlock,但我怀疑它会移植过来
但是,请记住,Android 2.x上的按钮背景与3.0+上的按钮背景看起来有点不同,因此您可能需要做更多的工作,以使操作栏空间中的按钮看起来符合您的需要。以下是xml中的实际操作方式 使用Roman的蜂窝式及以上布局 /layout-v11/actionbar\u custom\u view\u done\u 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>
<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-v11/actionbar\u discard\u 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>
<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>
<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>
<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>
/layout-v11/actionbar\u done\u 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>
<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>
<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>
<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>
并使用ActionBar Sherlock对其进行后传
/布局/操作栏\自定义\视图\完成\放弃.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>
<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"
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>
<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>
<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>
<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>
/布局/actionbar\u done\u 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>
<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>
<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>
<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>
要完成使用说明@Vlasto Benny Lava,这里是实际设置ActionBar的代码(基于Roman Nurik的for 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)
我将其用于片段
,因此使用getSherlockActivity()
。如果在活动中使用
,您可以自由省略该部分
您可以在两个位置设置此自定义操作栏:
onCreate()
(或在片段onAttach()
)中onCreateOptions菜单()
中关于如何将ActionBar恢复为“原始”形式的过程(选择完成或放弃后),您可以参考。我已实施并将Textview更改为按钮。现在我不知道在哪里定义按钮的onClick方法。好吧,根据这个例子,一切都是一样的