【问题标题】:Contextual Action menu positioned above the Action Bar in a Fragment上下文操作菜单位于片段中操作栏上方
【发布时间】:2018-06-08 05:43:01
【问题描述】:

我有一个包含学生列表视图的片段的活动。当我长按时,我想在视觉上超过操作栏显示操作菜单。我能够显示菜单,但是,它的位置没有超过操作栏。看图1,我想要图2中的东西。

我找到了this question,并尝试了他们的建议。但仍然没有运气。

这是我的 Fragment 类;

public class StudentsFragment extends Fragment
{
 List<Student> studentList
 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) 
 {
    studentView = inflater.inflate(R.layout.students_layout,container,false);
    studentList = ((MainActivity) getActivity()).getStudentList();
    displayStudents(this.getActivity());
    return studentView;
   }

private void displayStudents(Context context)
{

  final StudentsAdapter studentsAdapter;
  final ListView listView;
  ...
  studentsAdapter = new StudentsAdapter(getActivity(), studentList);
    listView = (ListView) 
  studentView.findViewById(R.id.students_student_list);
  listView.setAdapter(studentsAdapter);

  listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
            @Override
            public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
                // Capture total checked items
                final int checkedCount = listView.getCheckedItemCount();
                // Set the CAB title according to total checked items
                mode.setTitle(checkedCount + " Selected");
                // Calls toggleSelection method from ListViewAdapter Class
                studentsAdapter.toggleSelection(position);
            }

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

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                switch (item.getItemId()) {
                    // Calls getSelectedIds method from ListViewAdapter Class

                    case R.id.group_email:
                        ...
                        mode.finish();
                        return true;
                    default:
                        return false;
             }
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {
                studentsAdapter.removeSelection();
            }
        });
  }
}

这是我主要活动的主题;

    <style name="AppTheme.NoActionBar">
     <item name="windowActionBar">false</item>
     <item name="windowNoTitle">true</item>
     <item name="android:windowActionModeOverlay">true</item>
    </style>

这是必需的菜单;

<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <item
    android:id="@+id/group_email"
    android:title="Email"/>
  <item
    android:id="@+id/bulk_sms"
    android:title="SMS"/>
</menu>

非常感谢任何帮助或想法。

【问题讨论】:

    标签: android listview android-fragments contextmenu


    【解决方案1】:

    在您的AppTheme.NoActionBar 中添加:

        <item name="windowActionModeOverlay">true</item>
        <item name="actionModeBackground">@color/colorPrimary</item>
    

    您无需添加android:windowActionModeOverlay 只需windowActionModeOverlay

    【讨论】:

    • 是的!你拯救了我的一天。我在我的项目中使用了最新的 AppCompat,因此无需在属性中添加“android:”前缀。
    猜你喜欢
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多