如何设置两个自定义动作条按钮,在左,右的Android? [英] How to set two custom actionbar button in left and right in android?
问题描述
我使用的夏洛克库,我也有我自己的工具。我的问题是,我增加了两个项目的菜单,现在我想在动作条的左边第1项和第二的动作条的右侧。如何应对呢?
I am Using Sherlock library and i also implement with myself. My problem is that i added two items in menu, now i want 1 item in left of actionbar and second in right of actionbar. How to to it?
推荐答案
您可以创建这样的操作栏,但它多一点膨胀菜单更为复杂。在 onCreateOptionsMenu创建菜单()
方法将被永远靠右侧,放置在拆分操作栏,或隐藏在菜单键。
You can create such action bar, but it's little more complicated than inflating a menu. Menu created in onCreateOptionsMenu()
method will be always aligned to right, placed in split action bar or hidden under the menu key.
如果你想你的动作栏仅包含两个菜单项 - 一个在左边,另一个在右边 - 你必须建立操作栏中的自定义视图
If you want your action bar to contain just two menu items - one on the left edge and the other on the right edge - you have to create custom view in the action bar.
在自定义视图的布局将是这样的:
The custom view layout will be something like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton android:src="@drawable/ic_menu_item1"
android:background="?attr/actionBarItemBackground"
android:layout_width="?attr/actionBarSize"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:id="@+id/item1"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"/>
<ImageButton android:src="@drawable/ic_menu_item2"
android:background="?attr/actionBarItemBackground"
android:layout_width="?attr/actionBarSize"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:id="@+id/item2"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"/>
</RelativeLayout>
定义中要使用的自定义视图的主题。主题应包括:
Define in the theme that you want to use custom view. The theme should contain:
<style name="MyTheme" parent="@style/Theme.Sherlock">
<item name="actionBarStyle">@style/MyActionBarStyle</item>
<item name="android:actionBarStyle">@style/MyActionBarStyle</item>
</style>
<style name="MyActionBarStyle" parent="@style/Widget.Sherlock.ActionBar">
<item name="displayOptions">showCustom</item>
<item name="android:displayOptions">showCustom</item>
</style>
将在活动(或片段)的自定义视图:
Set the custom view in the activity (or fragment):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar ab = getSherlock().getActionBar();
LayoutInflater li = LayoutInflater.from(this);
View customView = li.inflate(R.layout.my_custom_view, null);
ab.setCustomView(customView);
ImageButton ibItem1 = (ImageButton) customView.findViewById(R.id.item1);
ibItem1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// ...
}
});
ImageButton ibItem2 = (ImageButton) customView.findViewById(R.id.item2);
ibItem2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// ...
}
});
}
您必须添加点击侦听器的菜单项。不能使用 onOptionsItemSelected()
在这种情况下。
You have to add the click listeners for the menu items. You cannot use onOptionsItemSelected()
in this case.
这篇关于如何设置两个自定义动作条按钮,在左,右的Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!