Building ActionMode with custom layout in ActionBa

2019-02-21 22:10发布

I just starting using ActionBarSherlock for building some simple app, in my first screen I have simple list and I added new menu item for adding new item to the list:

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

now when user choose to add a new item I want to start a new action mode for adding new item, this action mode should contain a simple layout with text box and a button, so I created this layout:

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

so now I just need to set this layout to the bar in the new action mode:

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

and in my action:

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

So basically I need something between ActionModes and CustomNavigation from the sherlock example, but the problem is that it set the layout to the main bar and not for the new bar that open in for action.

any suggestions?

1条回答
SAY GOODBYE
2楼-- · 2019-02-21 22:40

You probably want to use the method in the ActionMode class called "setCustomView" .

so something like this:

newItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            actionMode = startActionMode(new MyAction(ListEditor.this));
            actionMode.setCustomView(customNav);
            return true;
        }
    });
查看更多
登录 后发表回答