位于片段中操作栏上方的上下文操作菜单
Contextual Action menu positioned above the Action Bar in a Fragment
我有一个 Activity 的片段,其中包含学生的 ListView。当我长按时,我想显示一个 Action 菜单,视觉上超过 Action Bar。我能够显示菜单,但是,它的位置没有超过操作栏。看图 1,我想要图 2 中的东西。
我找到了 并尝试了他们的建议。但是还是不行。
这是我的片段class;
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();
}
});
}
}
这是我的主要activity主题;
<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>
非常感谢任何帮助或想法。
在您的 AppTheme.NoActionBar
中添加:
<item name="windowActionModeOverlay">true</item>
<item name="actionModeBackground">@color/colorPrimary</item>
您不需要添加 android:windowActionModeOverlay
它只是 windowActionModeOverlay
我有一个 Activity 的片段,其中包含学生的 ListView。当我长按时,我想显示一个 Action 菜单,视觉上超过 Action Bar。我能够显示菜单,但是,它的位置没有超过操作栏。看图 1,我想要图 2 中的东西。
我找到了
这是我的片段class;
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();
}
});
}
}
这是我的主要activity主题;
<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>
非常感谢任何帮助或想法。
在您的 AppTheme.NoActionBar
中添加:
<item name="windowActionModeOverlay">true</item>
<item name="actionModeBackground">@color/colorPrimary</item>
您不需要添加 android:windowActionModeOverlay
它只是 windowActionModeOverlay