搜索框小部件:将搜索图标移到右边

3

我有一个SearchView,我想把搜索图标移动到右边(如果可能)enter image description here

我的xml代码:

 <android.widget.SearchView
    android:id="@+id/searchBox"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginBottom="7dp"
    android:layout_below="@+id/header"
    android:iconifiedByDefault="false"
    android:background="@drawable/searchrectangle"
    android:queryHint="@string/search_message">

</android.widget.SearchView>
2个回答

2

我希望这个工作。它对我有用。

android:layoutDirection="rtl"

0
如果我没记错的话,这是搜索视图的源代码 https://android.googlesource.com/platform/frameworks/support.git/+/jb-mr2-release/v7/appcompat/res/layout/abc_search_view.xml
从外观上看,它使用线性布局,并且提到的图标在文本前面。这意味着没有很好的方法将其放在右侧。
基本上还剩下3个选项,或者至少是我想到的。
  1. 使用自定义布局与drawable和edittext,我认为有些效果可以使用CollapsibleActionView或/和自定义动画来实现(这取决于你需要什么)

  2. 使用搜索视图,没有文字图标的样式:

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:searchViewStyle">@style/appTheme.SearchView</item> </style> <style name="AppTheme.SearchView" parent="Widget.AppCompat.SearchView"> <item name="android:searchIcon" tools:targetApi="lollipop">@null</item> </style>

然后基本上像平常一样在右侧添加图标。

  1. 扩展搜索视图并在构造函数中填充自定义布局(未测试此方法)

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