如何在任何给定的操作栏图标上添加下拉菜单

时间:2014-07-07 02:30:39

标签: java android xml android-layout android-actionbar

当我点击我的操作栏图标并将某些选择链接到特定活动时,我一直在尝试创建一个简单的下拉菜单。

这样的事情:

enter image description here

我试图遵循这个但没有运气。

Android ActionBar ActionProvider submenu

How to add submenu items to ActionBar action in code?

目前我将此作为我的Java代码,但下拉菜单显示在我的appname下,而不是溢出图标:

    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        super.onCreate(savedInstanceState);

        SpinnerAdapter adapter =
                ArrayAdapter.createFromResource(this, R.array.actions,
                        android.R.layout.simple_spinner_dropdown_item);

// Callback
        OnNavigationListener callback = new OnNavigationListener() {

            String[] items = getResources().getStringArray(R.array.actions); // List items from res

            @Override
            public boolean onNavigationItemSelected(int position, long id) {

                // Do stuff when navigation item is selected

                Log.d("NavigationItemSelected", items[position]); // Debug

                return true;

            }

        };

1 个答案:

答案 0 :(得分:0)

您可以为此使用“首选项活动”。所以只需在res文件夹中创建一个xml文件夹。并创建一个settings.xml

<?xml version="1.0" encoding="utf-8"?>

<com.xxx.MyPreferenceCategory
     android:title="@string/about_application" >

    <Preference
        android:key="shareApp"
        android:summary="@string/share_app_summary"
        android:title="@string/share_app" />

    <Preference
        android:key="rateApp"
        android:summary="@string/rate_app_summary"
        android:title="@string/rate_app" />

    <Preference
        android:key="version"
        android:summary="@string/version_app_summary"
        android:title="@string/version_app" />
</com.xxx.MyPreferenceCategory>

<com.xxx.MyPreferenceCategory 
    android:title="@string/about_developer" >

    <Preference
        android:key="contactUs"
        android:summary="@string/contact_us_summary"
        android:title="@string/contact_app" />

    <Preference
        android:key="aboutUs"
        android:title="@string/about_app" />

    <Preference
        android:key="terms"
        android:title="@string/about_app_terms" />

    <Preference
        android:key="policy"
        android:title="@string/about_app_policy" />
</com.xxx.MyPreferenceCategory>

之后创建一个MyPreferenceCategory.java。

public class MyPreferenceCategory extends PreferenceCategory{

public MyPreferenceCategory(Context context) {
    super(context);
}

private TextView categoryTitle;

public MyPreferenceCategory(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public MyPreferenceCategory(Context context, AttributeSet attrs,
        int defStyle) {
    super(context, attrs, defStyle);
}
@Override
protected View onCreateView(ViewGroup parent) {
    categoryTitle  = (TextView) super.onCreateView(parent);

    return categoryTitle;
}

}

之后创建一个MenuOptions.java文件

public class MenuOptions extends SherlockPreferenceActivity {
Handler disconnectHandler;
Runnable disconnectCallback;
Preference shareApp, rateApp, contactUs, aboutUs, termsCondition,
        privacyPolicy;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.settings);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    findViewById(android.R.id.list).setBackgroundColor(Color.WHITE);

    disconnectHandler = new Handler() {
        public void handleMessage(Message msg) {
        }
    };

    disconnectCallback = new Runnable() {
        @Override
        public void run() {
        }
    };

    shareApp = (Preference) findPreference("shareApp");
    shareApp.setOnPreferenceClickListener(new OnPreferenceClickListener() {

        @Override
        public boolean onPreferenceClick(Preference preference) {
        //do your stuff
            return false;
        }
    });

    rateApp = (Preference) findPreference("rateApp");
    rateApp.setOnPreferenceClickListener(new OnPreferenceClickListener() {

        @Override
        public boolean onPreferenceClick(Preference preference) {
            //do your stuff
            return false;
        }
    });

    contactUs = (Preference) findPreference("contactUs");
    contactUs.setOnPreferenceClickListener(new OnPreferenceClickListener() {

        @Override
        public boolean onPreferenceClick(Preference preference) {
            //do your stuff
            return false;
        }
    });

    aboutUs = (Preference) findPreference("aboutUs");
    aboutUs.setOnPreferenceClickListener(new OnPreferenceClickListener() {

        @Override
        public boolean onPreferenceClick(Preference preference) {
            return false;
        }
    });

    termsCondition = (Preference) findPreference("terms");
    termsCondition
            .setOnPreferenceClickListener(new OnPreferenceClickListener() {

                @Override
                public boolean onPreferenceClick(Preference preference) {
                    return false;
                }
            });

    privacyPolicy = (Preference) findPreference("policy");
    privacyPolicy
            .setOnPreferenceClickListener(new OnPreferenceClickListener() {

                @Override
                public boolean onPreferenceClick(Preference preference) {
                    return false;
                }
            });
}

@Override
public boolean onOptionsItemSelected(
        com.actionbarsherlock.view.MenuItem item) {
    // TODO Auto-generated method stub
    switch (item.getItemId()) {
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        break;

    default:
        break;
    }
    return super.onOptionsItemSelected(item);
}

}

在那之后,您在使用操作栏设置按钮的位置放置此代码。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.activity_main, menu);
    return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_settings:
        Intent intent = new Intent(getBaseContext(), MenuOptions.class);
        startActivity(intent);
        return true;

    default:
        break;

    }
    return super.onOptionsItemSelected(item);
}