使用“菜单按钮”方法。通常,在英语中,该按钮应称为“设置”,而不是“首选项”,即使它管理首选项。傻,我知道。
关于没有菜单按钮的手机:
唯一不运行 Android 4.0 或更高版本的手机。只要您的项目构建中的目标 Android 3.0 或更高版本,并正确创建菜单项(如果您需要知道如何创建,我将提供一个片段),Android 将检测到没有硬件菜单按钮并添加“溢出”菜单到操作栏。
编辑:这是片段:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem buttonSettings = menu.add("Settings"); // This is a hardcoded string. When you get around to it, switch it to a localized String resource.
buttonSettings.setIcon(R.drawable.ic_settings); // I have a custom icon for this. You can use the one in android.R.drawable, but I don't know its name right now.
buttonSettings.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); // This forces it to go in the overflow menu, which is preferred.
buttonSettings.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Intent settingsIntent = new Intent(YourActivity.this, Preferences.class); // Change YourActivity to.. well, your activity. Change Preferences to the name of your Settings activity.
YourActivity.this.startActivity(settingsIntent);
return false; // I honestly don't know why this should return false, but every example I've seen has it do so. So I'd leave it in.
}
});
return true;
}
溢出菜单看起来像三个点,就像操作栏的“更多...”选项。Android 设计指南说您应该始终将设置选项放在溢出菜单中。
希望能解惑,有什么其他的,欢迎提问。