答案
要在ActionBarSherlock中向ActionBar添加按钮,您需要遵循以下步骤:
res/layout/
目录下创建一个XML布局文件(例如:action_bar_button.xml
),并添加一个Button
控件。<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/action_bar_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:text="Button at the bottom" />
</RelativeLayout>
onCreate()
方法中,通过ActionBarSherlock
提供的setSupportActionbar()
方法来设置ActionBar。import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.ActionMode;
...
public class MainActivity extends SherlockActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set the action bar
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setTitle("My Title");
}
}
res/values/
目录下创建一个名为action_mode_theme.xml
的文件,并添加以下内容:<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Sherlock.Light" parent="Theme.Sherlock.Light.DarkActionBar">
<item name="actionBarStyle">@style/Widget.Sherlock.Light.ActionBar.Solid.Inverse</item>
<item name="actionBarTabStyle">@style/Widget.Sherlock.Light.ActionBar.TabView.Inverse</item>
<item name="actionBarItemBackground">@drawable/abs__item_background_holo_light</item>
<item name="actionModeStyle">@style/Widget.Sherlock.Light.ActionMode.Inverse</item>
<item name="actionModeSplitBackground">@drawable/abs__action_bar_item_split_background_holo_light</item>
<item name="actionModeCloseButtonStyle">@style/Widget.Sherlock.Light.ActionMode.CloseButton.Inverse</item>
</style>
</resources>
onCreateActionMode()
方法中,通过ActionBarSherlock
提供的buildActionMode()
方法来创建一个操作模式。
import com.actionbarsherlock.app.SherlockActionMode;
import com.actionbarsherlock.app.SherlockActivity;
...
public class MainActivity extends SherlockActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set the action bar
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setTitle("My Title");
// Create the action mode
mActionMode = getSupportActionBar().startActionMode(new MyActionMode(this));
}
@Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
private static class MyActionMode implements ActionMode.Callback {
private final SherlockActivity mActivity;
private final ActionBarSherlock mActionBar;
private final View mCustomView;
private final Menu mMenu;
public MyActionMode(SherlockActivity activity) {
mActivity = activity;
mActionBar = activity.getSupportActionBar();
mCustomView = LayoutInflater.from(mActivity).inflate(R.layout.action_mode_custom, null);
mMenu = mActivity.getMenu();
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mActionBar
领取专属 10元无门槛券
手把手带您无忧上云