【发布时间】:2019-12-02 11:09:17
【问题描述】:
所以我在 Styles.xml 中为 AppTheme NoActionBar 设置,因为我只希望工具栏出现在几个 Activites 中,并且我还创建了包含两个项目的 post_details_menu。但是工具栏根本没有显示。
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
然后在 Manifest.xml 中,我有主题:AppTheme
<application
android:theme="@style/AppTheme"
...>
</application>
然后我创建了 my_toolbar:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"/>
</LinearLayout>
我想在我的 PostDetailsActivity 中使用我的工具栏:
public class PostDetailActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_detail);
Toolbar toolbar = findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.post_details_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.post_Edit:
showMessage("post_edit clicked");
return true;
case R.id.post_Delete:
showMessage("post_delete clicked");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
最后这是我的 ActivityPostDetails 布局:
<androidx.core.widget.NestedScrollView
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PostDetailActivity"
android:background="#fff">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView ... />
<TextView ... />
<androidx.recyclerview.widget.RecyclerView .../>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
【问题讨论】:
标签: android android-recyclerview scrollview toolbar