Android AppCompat SearchView EditText在早期版本中缺少底部线条

5

我想在我的布局中使用SearchView(不是放在ToolBar或ActionBar中)。

<FrameLayout android:layout_width="0dp" android:layout_weight="50" android:layout_height="wrap_content" >
    <android.support.v7.widget.SearchView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|left" style="@style/MySearchViewStyle" />
  </FrameLayout>

我正在使用AppCompat,但在Android 5.0以下的设备上显示效果不同。EditText控件在这些设备上缺少底部边框。
Android 5.0及以上版本的搜索框能够正确地显示底部边框: Lollipop SearchView with bottom border 而在Android 5.0以下版本的搜索框则没有底部边框: Pre-lollipop SearchView with NO bottom border 我的主题是基础主题:
    <style name="Theme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- TOOLBAR -->
     <item name="android:windowNoTitle">true</item>
     <item name="windowActionBar">false</item>
     <item name="windowNoTitle">true</item>
     <!-- Search -->
     <item name="searchViewStyle">@style/MySearchViewStyle</item>
     <item name="android:searchViewStyle">@style/MySearchViewStyle</item>
     <!-- COLOURS -->
     <item name="colorPrimary">@color/primary</item>
     <item name="colorPrimaryDark">@color/primary_dark</item>
     <item name="colorAccent">@color/accent</item>
  </style>
  <style name="MySearchViewStyle" parent="Widget.AppCompat.SearchView">
     <item name="android:editTextStyle">@style/EditText</item>
     <item name="editTextStyle">@style/EditText</item>
  </style>
  <style name="EditText">
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:inputType">text</item>
    <item name="android:cursorVisible">true</item>
    <item name="android:maxLength">1000</item>
    <item name="android:selectAllOnFocus">true</item>
    <item name="android:paddingTop">4dp</item>
  </style>

任何帮助使我的样式保持一致都将不胜感激,谢谢。

升级到最新的支持库后,注意到了同样的问题。以前的版本没有这样的问题。继续寻找解决方案。 - Eugene Popovich
啊,那可能就解释了,因为我没有旧版本,上周我才添加了设计库。:-( 如果你找到了解决方案,请告诉我。谢谢。 - Daisy
2个回答

6

从支持库版本25.0.0开始,对于不属于工具栏的SearchView,必须调用((View) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text).getParent()).setBackgroundResource(0); - 0101100101

0

更新:针对Kotlin + Androidx

searchView.findViewById<EditText>(androidx.appcompat.R.id.search_src_text).setBackgroundResource(R.drawable.abc_textfield_search_default_mtrl_alpha)

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