在Actionbar sherlock中为操作项/溢出添加自定义下拉式微调器

时间:2014-06-05 18:01:20

标签: android actionbarsherlock

我想得到像这张照片的效果。我正在使用Action bar Sherlock来实现向后兼容。

enter image description here

我在操作栏中使用 NAVIGATION_MODE_TABS 模式。我按照this博客来实现以下屏幕。我的代码在这里。

HomeActivity 扩展 SherlockFragmentActivity

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    actionBar = getSupportActionBar();

    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    // actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
    setTitle(R.string.app_name);

    mViewPager = (ViewPager) findViewById(R.id.pager);

    // Add the tabs
    mTabsAdapter = new TabsAdapter(this, actionBar, mViewPager);

    mTabsAdapter.addTab(actionBar.newTab().setText("General"),
            GeneralFragment.class, null);

    mTabsAdapter.addTab(actionBar.newTab().setText("Memory"),
            MemoryFragment.class, null);

    // more tabs here; seems no need those 

    if (savedInstanceState != null) {
        actionBar.setSelectedNavigationItem(savedInstanceState
                .getInt("tab"));
    }


    int titleId = Resources.getSystem().getIdentifier("action_bar_title",
            "id", "android");

    // If you're using sherlock, in older versions of android you are not
    // supposed to get a reference to android.R.id.action_bar_title, So
    // here's little hack for that.
    if (titleId == 0) {
        titleId = com.actionbarsherlock.R.id.abs__action_bar_title;
    }

    View titleView = findViewById(titleId);

    // attach listener to this spinnerView for handling spinner selection
    // change
    spinnerView = (Spinner) getLayoutInflater().inflate(
            R.layout.spinner_layout, null);
    //
    // source of ViewGroupUtils class is given at the end of this post.
    ViewGroupUtils.replaceView(titleView, spinnerView);

}

我设法建立了这个。enter image description here

我如何在ActionbarSherlock中构建我想要的屏幕?任何链接或建议将不胜感激。

已编辑:我的ViewGroupUtils类  `public class ViewGroupUtils {

public static ViewGroup getParent(View view) {
    return (ViewGroup) view.getParent();
}

public static void removeView(View view) {
    ViewGroup parent = getParent(view);
    if (parent != null) {
        parent.removeView(view);
    }
}

public static void replaceView(View currentView, View newView) {

    ViewGroup parent = getParent(currentView);
    if (parent == null) {
        return;
    }
    final int index = parent.indexOfChild(currentView);
    removeView(currentView);
    removeView(newView);
    parent.addView(newView, index);
}

} `

1 个答案:

答案 0 :(得分:1)

只需使用此

更改菜单XML即可
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/action_share"
    android:icon="@android:drawable/ic_menu_share"
    android:orderInCategory="100"
    android:showAsAction="ifRoom"
    android:title="Share"/>
<item
    android:id="@+id/action_search"
    android:icon="@android:drawable/ic_menu_search"
    android:orderInCategory="100"
    android:showAsAction="ifRoom"
    android:title="Search"/>

<item
    android:id="@+id/more"
    android:orderInCategory="100"
    android:icon="@android:drawable/ic_menu_more"
    android:showAsAction="always"
    android:title="">
    <menu>
        <item
            android:id="@+id/action_call"
            android:icon="@android:drawable/ic_menu_call"
            android:orderInCategory="100"
            android:showAsAction="never"
            android:title="Call"/>
        <item
            android:id="@+id/action_camera"
            android:icon="@android:drawable/ic_menu_camera"
            android:orderInCategory="100"
            android:showAsAction="never"
            android:title="Camera"/>
        <item
            android:id="@+id/action_compass"
            android:icon="@android:drawable/ic_menu_compass"
            android:orderInCategory="100"
            android:showAsAction="never"
            android:title="Compass"/>
        <item
            android:id="@+id/action_help"
            android:icon="@android:drawable/ic_menu_help"
            android:orderInCategory="100"
            android:showAsAction="never"
            android:title="Help"/>
    </menu>
</item>

无需更改Java文件。 输出是....

enter image description here