我有一个Android应用程序,其中我有一个Main布局,其中包含支持工具栏作为布局的一部分使用:
<include layout="@layout/toolbar"
android:layout_width="match_parent"
android_layout_height="wrap_content" />
它包含的工具栏如下:
布局/ toolbar.xml
<TextView
android:id="@+id/txtViewHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="@string/main_tab_status"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold"
android:textSize="28sp"
android:ellipsize="end"
android:lines="1"
android:gravity="center"
android:layout_gravity="center" />
</android.support.v7.widget.Toolbar>
然后在我的主要活动中,我在onCreateOptionsMenu
方法中为我的toolbar_menu XML充气,如下所示:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.toolbar_menu, menu);
btnShare = menu.findItem(R.id.btnShare);
btnSelectAll = menu.findItem(R.id.btnSelectAll);
btnDelete = menu.findItem(R.id.btnDelete);
return true;
}
但是,当我尝试在ListView
模式下使用CHOICE_MODE_MULTIPLE_MODAL
时,它还有一个onCreateActionMode
方法。我想我会做我以前做过的事情,我再次用这个菜单充气:
@Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
actionMode.getMenuInflater().inflate(R.menu.toolbar_menu, menu);
return true;
}
但是这会导致另一个菜单显示在我的主工具栏菜单上方,如下面的屏幕截图所示。
我不想显示第二个菜单,我希望它使用我的Main布局中的现有工具栏。我怎么能做到这一点?
My Main.java(主要活动)被定义为AppCompatActivity
,其中Messages.java是加载到Main活动中的Fragment。
答案 0 :(得分:2)
使用NoActionBar创建样式:
<!-- Base application theme. -->
<style name="AppThemeNoActonBar" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
在活动代码中的AndroidManifest.xml中使用此样式:
<activity
android:name=".MainActivity"
android:theme="@style/AppThemeNoActonBar" />
然后将工具栏添加到您的活动布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="wrap_content"
android:layout_height="56dp" />
<RelativeLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar">
<!--YOUR LAYOUT CODE-->
</RelativeLayout>
然后初始化活动的java代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
Toolbar toolbar=findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
答案 1 :(得分:0)
在清单中的活动中不添加操作栏主题。
<activity android:name=".YourActivity"
android:theme="@style/YourThemeNoActionBar"/>