在android sdk的searchview中更改文本字段的背景

pdc*_*dcc 1 java xml android android-activity searchview

我正在制作一个应用程序,并且我有一个带有搜索视图的活动。我的观点是我想更改文本字段的背景颜色(屏幕截图中的黑色矩形)。

我的可绘制:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
         android:shape="rectangle"
         android:padding="10dp">

 <solid android:color="#FFFFFF"/>

    <corners
 android:bottomRightRadius="10dp"
 android:bottomLeftRadius="10dp"
 android:topLeftRadius="10dp"
     android:topRightRadius="10dp"/>
</shape>
Run Code Online (Sandbox Code Playgroud)

我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".SearchActivity" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="160sp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/search_background"
    android:orientation="vertical" >

    <SearchView
        android:id="@+id/searchView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20sp"
        android:layout_marginRight="20sp"
        android:layout_marginTop="60sp"
        android:actionViewClass="android.widget.SearchView"
        android:background="@drawable/searchview"
        android:iconifiedByDefault="false"
        android:paddingLeft="10sp"
        android:paddingTop="5sp"
        android:scrollbarAlwaysDrawVerticalTrack="true"
        android:showAsAction="collapseActionView"
        android:focusable="false" >

    </SearchView>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

截图:http : //imageshack.com/a/img829/3203/pd85.png

有人可以帮我吗?谢谢

Jua*_*tés 5

一点侦探工作后,你会发现,如果你看一下SearchView小部件,你会看到它扩展LinearLayout了一些附加功能。查看#242它包含一个小部件的行(特别是 a SearchAutoComplete,它扩展AutoCompleteTextView了到底扩展了EditText.

话虽如此,您需要修改该子元素的背景属性,如下所示:

SearchView c = findViewById(R.id.searchView); 
EditText e = (EditText)c.findViewById(c.getContext().getResources().getIdentifier("android:id/search_src_text", null, null));
         e.setBackgroundColor(Color.BLACK); //?If you just want a color
         e.setBackground(getResources().getDrawable(R.drawable.YOUR_DRAWABLE));
         //? If you want a drawable ? 
Run Code Online (Sandbox Code Playgroud)

有用的链接: