我已经完成了https://developer.android.com/training/appbar文档中提到的所有内容。
这是onCreate方法
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getLocationBtn = findViewById(R.id.get_location_btn);
saveLocationBtn = findViewById(R.id.save_btn);
longText = findViewById(R.id.long_text);
latText = findViewById(R.id.lat_text);
attractionNameText = findViewById(R.id.location_name_text);
ratingBar = findViewById(R.id.ratingBar);
detailsText = findViewById(R.id.detailsText);
getViewSavedLocationBtn = findViewById(R.id.view_saved_location_btn);
attractionTypeText = findViewById(R.id.attractionTypeText);
updateToFirebaseBtn = findViewById(R.id.update_to_firebase);
db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "app_database")
.allowMainThreadQueries()
.build();
getViewSavedLocationBtn.setOnClickListener(this);
updateToFirebaseBtn.setOnClickListener(this);
toolbar = findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
}
这是app_bar xml文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/app_bar"
android:background="@color/colorAccent"
>
</android.support.v7.widget.Toolbar>
这是main_manu.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/enter_attraction"
android:icon="@drawable/upload_icon"
android:title="@string/upload_icon_title"
app:showAsAction="always"
/>
</menu>
模拟器上的输出
答案 0 :(得分:0)
在活动中添加getSupportActionBar().setDisplayHomeAsUpEnabled(true)
在片段中将setHasOptionsMenu(true);
添加到onCreate
此问题已解决