一,Android应用栏右上角的设置按钮(菜单项),点击后进入一个新的设置界面
首先在Earthquake应用中的res/menu/main.xml
中添加
菜单栏样式
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.android.quakereport.EarthquakeActivity">
<item
android:id="@+id/action_settings"
android:title="@string/settting_menu_item"
android:icon="@mipmap/ic_launcher"
android:orderInCategory="1"
app:showAsAction="ifRoom"/>
</menu>
更新strings.xml在res/values/strings.xml
文件中添加一些字符串,更新后的strings.xml
<resources>
<string name="app_name">Earthquake Report</string>
<string name="near_the">Near the</string>
<string name="settting_menu_item">Setting</string>
<string name="settings_title">Earthquake Setting</string>
<string name="setting_min_magnitude_label">Choose Minimum Magnitude</string>
<string name="setting_min_magnitude_key" translatable="false">min_magnitude</string>
<string name="setting_min_magnitude_default" translatable="false">6</string>
<string name="setting_location">Location</string>
<string name="setting_location_default">world</string>
<string name="setting_date">Date Setting</string>
<string name="setting_date_default">1</string>
</resources>
覆盖 EarthquakeActivity.java 中的onCreateOptionMenu方法和onOptionItemSelected方法以使用 菜单,然后在用户单击菜单项时作出响应,进入一个新的设置界面
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
Intent settingsIntent = new Intent(this, SettingsActivity.class);
startActivity(settingsIntent);
return true;