【问题标题】:android searchview grey border around input field API >= 26输入字段API周围的android searchview灰色边框> = 26
【发布时间】:2018-09-13 11:17:51
【问题描述】:

使用 API >= 26 的模拟器,搜索视图的输入文本字段周围出现奇怪的灰色边框。只有片段内的 SearchViews 会遇到此问题。

您能告诉我如何解决这个问题吗?

SearchView Picture

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/view_layout"
            android:paddingLeft="0dp"
            android:paddingRight="0dp"
            android:paddingTop="0dp"
            android:paddingBottom="0dp"
            android:background="@color/grey_background"
            android:clickable="true"
            android:focusableInTouchMode="true">
<RelativeLayout
    android:id="@+id/search_view_layout"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:background="@color/grey_background">
    <SearchView
        android:id="@+id/search_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:iconifiedByDefault="false"
        android:padding="2dp"
        android:layout_margin="5dp"
        android:layout_marginLeft="0dp"
        android:queryHint="Search...."
        android:elevation="0dp"
        android:background="@drawable/search_view_border"
        android:focusable="false"/>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recycler_view"
    android:paddingTop="5dp"
    android:background="@color/grey_background"
    android:layout_below="@+id/search_view_layout"
    android:clickable="true"
    android:focusableInTouchMode="true"
    android:focusable="true" />
</RelativeLayout>

search_view_border.xml 的代码

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >    
    <item>
        <shape>
            <!-- set the shadow color here -->
            <stroke
                android:width="10dp"
                android:color="#A1A9B4" />    
            <!-- setting the thickness of shadow (positive value will give shadow on that side) -->
            <padding
                android:bottom="1dp"
                android:left="1dp"
                android:right="1dp"
                android:top="1dp" />
            <corners android:radius="0dp"/>
        </shape>
    </item>
    <!-- Background -->
    <item>
        <shape>
            <solid android:color="#ffffff" />
            <corners android:radius="0dp" />
        </shape>
    </item>
</layer-list>

【问题讨论】:

  • 能否包含 search_view_border 的 XML?
  • @Dan XML for search_view_border 添加。
  • 你在真机上试过了吗?
  • @Quicklearner 我没有在带有 api >= 26 的真实设备上尝试过这个
  • 请在真机上试用并检查行为

标签: android android-fragments android-viewpager searchview


【解决方案1】:

试试这个想法

 <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:background="@drawable/border_shape_grey">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:layout_gravity="center"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:orientation="horizontal">
            <ImageView
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_gravity="center"
                android:src="@drawable/ic_search"/>
            <EditText
                style="@style/TextAppearance.AppCompat"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:hint="@string/action_search"
                android:focusableInTouchMode="true"
                android:focusable="true"
                android:descendantFocusability="blocksDescendants"
                android:inputType="text"
                android:singleLine="true"
                android:background="#00000000"
                android:padding="8dp"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </FrameLayout>

border_shap_grey.xml

<?xml version="1.0" encoding="utf-8"?>

<stroke android:width="2dp"
    android:color="#f5f6f6"
    />
<solid android:color="#f5f6f6"/>

<padding android:left="1dp"
    android:top="1dp"
    android:right="1dp"
    android:bottom="1dp"
    />

<corners
    android:radius="5dip"/>

【讨论】:

  • 感谢您的建议;如果没有修复使用 SearchView 的问题,我会试一试
【解决方案2】:

我发现的这个问题的解决方案:

  1. 将 searchview 小部件更改为 android.support.v7.widget.SearchView
  2. 检索 search_plate 布局并将其高度设置为 0。

    LinearLayout searchPlate = (LinearLayout) searchView.findViewById(android.support.v7.appcompat.R.id.search_plate); searchPlate.setElevation(0f);

参考: Changing the background drawable of the searchview widget中接受的答案

【讨论】:

    猜你喜欢
    • 2023-03-07
    • 1970-01-01
    • 2020-06-24
    • 2017-01-02
    • 2011-01-22
    • 2018-10-04
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多