【问题标题】:ListView Multiselect onCreateActionMode causing 2 Actionbars to appearListView 多选 onCreateActionMode 导致出现 2 个操作栏
【发布时间】:2018-05-21 13:02:03
【问题描述】:

我有一个 Android 应用程序,其中我有一个主布局,其中包含一个支持工具栏作为布局的一部分,使用:

<include layout="@layout/toolbar"
     android:layout_width="match_parent"
     android_layout_height="wrap_content" />

它包含的工具栏如下:

layout/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 方法中膨胀我的工具栏菜单 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;
}

但是,当我尝试在CHOICE_MODE_MULTIPLE_MODAL 模式下使用我的ListView 时,它也有一个onCreateActionMode 方法。我想我会做我以前做的,我用这个再次膨胀菜单:

@Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
     actionMode.getMenuInflater().inflate(R.menu.toolbar_menu, menu);
     return true;
}

但是,这会导致另一个菜单出现在我的主工具栏菜单上方,如下面的屏幕截图所示。

我不希望出现第二个菜单,我希望它使用我主布局中的现有工具栏。我怎样才能做到这一点?

我的 Main.java(主要活动)被定义为 AppCompatActivity,其中 Messages.java 是加载到主要活动中的片段。

【问题讨论】:

    标签: android android-layout android-actionbar


    【解决方案1】:

    使用 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标签中使用这个样式:

    <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>
    

    然后为activity初始化java代码:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);
        Toolbar toolbar=findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }
    

    【讨论】:

      【解决方案2】:

      在清单中的 Activity 中不添加任何操作栏主题。

      <activity android:name=".YourActivity"
           android:theme="@style/YourThemeNoActionBar"/>
      

      【讨论】:

      • 谢谢,不过我已经将我的 Activity 定义为 @style/Theme.AppCompat.NoActionBar。
      猜你喜欢
      • 1970-01-01
      • 2016-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多