与操作按钮Android的自定义操作栏操作、自定义、按钮、Android

2023-09-12 03:12:34 作者:满江风月.

下面是我要创建的操作栏。

Here is the action bar that I want to create.

下面是我把菜单项。

   <item
        android:id="@+id/search"
        android:title="Search"
        android:showAsAction="ifRoom|collapseActionView"
        android:actionLayout="@layout/search_layout"
    />

     <item
        android:id="@+id/search1"
        android:title="PHOTO"
        android:showAsAction="ifRoom"
        android:actionLayout="@layout/search_layout"
    />

collapseActionView - 崩溃在启动时搜索菜单。

collapseActionView - collapses search menu on startup.

点击搜索产品后,它显示了编辑文字像图片1.但我需要经常看到它,不仅点击后

After click on search item it shows edit text like on picture 1. But I need always see it, not only after click.

如果我将它设置为总是会弹出的菜单项,从因为FILL_PARENT的屏幕。

If I set it to Always it will pop the menu items from the screen because of fill_parent.

<EditText
        android:id="@+id/txt_search"
        android:inputType="text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />  

时有可能得到宽的标题栏布局没有菜单项?在这种情况下,我可以设置固定宽度的EditText项目。

Is it possible to get width of title bar layout without menu items? In this case I can set fixed width to edittext item.

这是任何其他方法,使工作? 在结果一定是自定义标题布局+的菜单项。该自定义布局必须填写菜单项和图标之间的空白区域。

Is it any other approaches to make it work? In result it must be custom title layout + menu items. This custom layout must fill empty space between menu items and icon.

推荐答案

您可以通过设置自定义视图代替操作栏标题做到这一点。

You can achieve this by setting a custom view in place of the action bar title.

ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.search_layout);
actionBar.setDisplayShowCustomEnabled(true);

其结果是的EditText ,填补了操作栏的整个宽度,除了操作按钮。它看起来酷似问题的第一张图像。

The result is an EditText that fills the entire width of the action bar, except for the action buttons. It looks exactly like the first image in the question.