我正在尝试使用标签实现ActionBarSherlock,但允许在某些标签上的操作栏中显示下拉菜单。我已经读过,获得两者的唯一方法是为下拉创建自定义actionProvider。这是我写的代码实现:
menu_with_dropdown.xml(带有单独的溢出菜单,工作正常)
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/motivate_picker"
android:showAsAction="always"
android:title="Right Here"
android:actionProviderClass=".ActionBarSpinner">
</item>
<item
android:id="@+id/menu_item"
android:showAsAction="always"
android:icon="@drawable/action_bar_plus_icon">
<menu>
<item android:id="@+id/menu_1"
android:title="@string/menu_1" />
<item android:id="@+id/menu_2"
android:title="@string/menu_2" />
<item android:id="@+id/menu_3"
android:title="@string/menu_3"/>
<item android:id="@+id/menu_4"
android:title="@string/menu_4"/>
</menu>
</item>
</menu>
使用此下拉列表的标签片段将此处充气
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu,inflater);
menu.clear();
getSherlockActivity().getSupportActionBar().setDisplayShowTitleEnabled(false);
getSherlockActivity().getSupportMenuInflater().inflate(R.menu.menu_with_dropdown, menu);
}
ActionBarSpinner.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import com.actionbarsherlock.view.ActionProvider;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.SubMenu;
public class ActionBarSpinner extends ActionProvider implements
MenuItem.OnMenuItemClickListener {
Context context_;
public ActionBarSpinner(Context context) {
super(context);
context_ = context;
}
@Override
public View onCreateActionView() {
LayoutInflater layoutInflater = LayoutInflater.from(context_);
View view = layoutInflater.inflate(R.layout.actionbar_menu,null);
//return view; //Will have formatted pulldown but not register clicks for submenu
return null; //Will register clicks and expand submenu but not be formatted as desired
}
@Override
public boolean onPerformDefaultAction() {
return false;
}
@Override
public boolean hasSubMenu() {
return true;
}
@Override
public void onPrepareSubMenu(SubMenu subMenu) {
MenuItem item = subMenu.add("Test1");
item.setOnMenuItemClickListener(this);
item = subMenu.add("Test3");
item.setOnMenuItemClickListener(this);
item = subMenu.add("Test2");
item.setOnMenuItemClickListener(this);
}
@Override
public boolean onMenuItemClick(MenuItem item) {
return true;
}
}
actionbar_menu.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
style="@android:style/Widget.ActionButton"
android:clickable="true">
<TextView
android:id="@+id/motivate_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Friend Picker"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:src="@drawable/switcher_arrow"/>
</LinearLayout>
因此,当我在ActionBarSpinner中调用onCreateActionView()时会出现问题。如果我返回膨胀的action of actionbar_menu.xml,它永远不会扩展我创建的subMenu。如果我在onCreateActionView()中返回null,当我点击它时子菜单会展开,但它没有我自己制作的自定义布局(它实际上什么都没有显示)。任何帮助将不胜感激。
答案 0 :(得分:0)
你必须返回null才能调用onPrepareSubMenu()。
然后,onCreateActionView()中的所有这些设置都需要在MainActivity中进入onCreateOptionsMenu()。
例如,
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
// Override the default language icon to the language is being used
MenuItem mI = menu.findItem(R.id.menu_locale);
Locale locale = Locale.getDefault();
String locLan = locale.getLanguage();
if (locLan.equals(Locale.CHINESE.getLanguage())) {
mI.setIcon(R.drawable.insready);
} else if (locLan.equals(Locale.ENGLISH.getLanguage())) {
mI.setIcon(R.drawable.ic_action_locale_en);
} else if (locLan.equals(Locale.FRENCH.getLanguage())) {
mI.setIcon(R.drawable.ic_action_search);
}
return true;
}