android - 在 ActionBarSherlock 中使用自定义布局构建 ActionMode

标签 android actionbarsherlock

我刚开始使用 ActionBarSherlock 构建一些简单的应用程序, 在我的第一个屏幕中,我有一个简单的列表,我添加了新的菜单项以将新项目添加到列表中:

MenuItem newItem = menu.add("New");
newItem.setIcon(R.drawable.ic_compose_inverse)
    .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

现在,当用户选择添加一个新项目时,我想启动一个新的操作模式来添加新项目,这个操作模式应该包含一个带有文本框和一个按钮的简单布局,所以我创建了这个布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

        <EditText
            android:id="@+id/text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:inputType="text" >
        </EditText>
        <Button
            android:id="@+id/addBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/add" />
</LinearLayout>

所以现在我只需要在新的操作模式下将这个布局设置为栏:

newItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                actionMode = startActionMode(new MyAction(ListEditor.this));
                return true;
            }
        });

在我的行动中:

private final class MyAction implements ActionMode.Callback {
    ...
    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        View customNav = LayoutInflater.from(context).inflate(R.layout.add_item, null);
        getSupportActionBar().setCustomView(customNav);
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        return true;
    }
}

所以基本上我需要 sherlock 示例中的 ActionModes 和 CustomNavigation 之间的东西,但问题是它将布局设置为主栏而不是为操作打开的新栏。

有什么建议吗?

最佳答案

您可能想使用 ActionMode 类中名为“setCustomView”的方法。

所以像这样:

newItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            actionMode = startActionMode(new MyAction(ListEditor.this));
            actionMode.setCustomView(customNav);
            return true;
        }
    });

关于android - 在 ActionBarSherlock 中使用自定义布局构建 ActionMode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10863080/

相关文章:

java - Android 没有选择 facebook 显示图像

android - 在android中更改选定的 ListView 项目图像问题

android - 如何获得正确的幻灯片以在android中单击按钮

android - 如何以编程方式更改 Sherlock 操作栏文本颜色?

android - 使 AppBarLayout/CollapsingToolbarLayout 对 ViewPager 中的 RecyclerView 中的滚动使用react

android - 直接在 Eclipse 中启动 Android Activity

android - 无法删除导致库版本冲突的外部库

android - 透明操作栏 : custom tabcolor

android - 在自定义 View 中显示 Action 的 Toast

安卓自定义主题: different theme for landscape and portrait mode