输入框API> = 26周围的android searchview灰色边框

时间:2018-09-13 11:17:51

标签: android android-fragments android-viewpager searchview

在使用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>

2 个答案:

答案 0 :(得分:0)

尝试这个想法

enter image description here

 <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"/>

答案 1 :(得分:0)

我发现的此问题的解决方案:

  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

中已接受的答案