Android SearchView - 将图标移到右侧(无需 ActionBar)

8

我有一个 Android SearchView,代码如下:

 <SearchView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:queryHint="Search the forum ..."
        android:iconifiedByDefault="true"
        android:id="@+id/search_forum"
        android:layout_alignParentRight="true"
        android:textColor="#aaa" />

这不在我的ActionBar中,而是在我的活动中的其他地方。

我需要图标(搜索图标)保留在布局的右侧。 单击后,它应扩展为SearchView(与ActionBar SearchView相同的行为)。 我尝试过android:layout_alignParentRight="true",但图标仍然停留在左侧。

任何帮助都将是伟大的 :)

7个回答

14

我找到了这个问题的解决方案。只需将布局方向设置为“rtl”,图标就会出现在右侧。以下是我的搜索视图代码,它按预期工作。

<SearchView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layoutDirection="rtl"
    android:iconifiedByDefault="true"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

左边的 x 选项不改变,有没有适当的解决方案? - nafees ahmed
是的,它正如你所说的那样工作。谢谢并继续保持,亲爱的。 - Pir Fahim Shah

2
我找不到确切的答案,于是我做了这个- 创建一个带有搜索图标的图像视图。将其放置在布局的右侧。 单击时,将搜索视图设置为可见并将iconified设置为false,并为搜索视图请求焦点。
如果有人能帮我找到合适的解决方案,我将非常感激。

2
如果问题仍未得到解答: 只需将SearchView移至RelativeLayout中,并将其与父元素的右侧对齐即可:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.SearchView
    android:layout_width="wrap_content"
    android:id="@+id/searchView"
    android:layout_alignParentRight="true"
    android:layout_height="match_parent" />

最终结果如下:
线性布局中包含对齐的SearchView的CardView。 点击此处查看效果图

0

试试这个:

 SearchView searchView =
       (SearchView) findViewById(R.id.search_view);
    searchView.setIconifiedByDefault(false);
    searchView.setIconified(false);

  ImageView searchViewIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);

    ViewGroup linearLayoutSearchView =
       (ViewGroup) searchViewIcon.getParent();
    linearLayoutSearchView.removeView(searchViewIcon);
    linearLayoutSearchView.addView(searchViewIcon);

来源于这里


它有效果了!这是什么魔法! - undefined

0

将SearchView作为菜单项添加而不是工具栏元素本质上解决了这个问题。

一个优雅的方法:

步骤1 - 将搜索字段添加到您的工具栏中:

<item
    android:id="@+id/action_search"
    android:icon="@android:drawable/ic_menu_search"
    app:showAsAction="always|collapseActionView"
    app:actionViewClass="android.support.v7.widget.SearchView"
    android:title="Search"/>

步骤2 - 在onCreateOptionsMenu()中添加逻辑

import android.support.v7.widget.SearchView; // not the default !

@Override
public boolean onCreateOptionsMenu( Menu menu) {
    getMenuInflater().inflate( R.menu.main, menu);

    MenuItem myActionMenuItem = menu.findItem( R.id.action_search);
    searchView = (SearchView) myActionMenuItem.getActionView();
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            UserFeedback.show( "SearchOnQueryTextSubmit: " + query);
            if( ! searchView.isIconified()) {
                searchView.setIconified(true);
            }
            myActionMenuItem.collapseActionView();
            return false;
        }
        @Override
        public boolean onQueryTextChange(String s) {
            // UserFeedback.show( "SearchOnQueryTextChanged: " + s);
            return false;
        }
    });
    return true;
}

来源:https://dev59.com/JV4c5IYBdhLWcg3w7t_E#34255229


1
原始问题说明:“我的ActionBar中没有它,它在我的Activity的其他地方。” OptionsMenu出现在工具栏/ ActionBar中。这只是通过利用OptionsMenu的默认位置来强制放置。它并没有解决问题。 - Abandoned Cart

0

我对放置在工具栏之外的SearchView有完全相同的要求,即当折叠时,搜索图标应位于右侧,而当展开时,它应覆盖整个宽度。

我已经尝试了在RelativeLayout中使用此解决方案来实现SearchView。

<androidx.appcompat.widget.SearchView
    android:id="@+id/searchView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true" />

在代码中处理了OnSearchClickListenerOnCloseListener,以在MATCH_PARENTWRAP_CONTENT之间切换宽度。

searchView.setOnSearchClickListener {
    val params = it.layoutParams as RelativeLayout.LayoutParams
    params.width = RelativeLayout.LayoutParams.MATCH_PARENT
    it.layoutParams = params
}

searchView.setOnCloseListener {
    val params = searchView.layoutParams as RelativeLayout.LayoutParams
    params.width = RelativeLayout.LayoutParams.WRAP_CONTENT
    searchView.layoutParams = params

    return@setOnCloseListener false
}

-1

对我来说这种方法很有效。我使用的是工具栏而不是操作栏。

searchView.setLayoutParams(new Toolbar.LayoutParams(Gravity.RIGHT)); 

他特别说了搜索视图不在工具栏/操作栏中。因为布局包含的SearchView不是派生自Toolbar,所以你的代码会抛出异常。 - vanomart

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接