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

问题描述 投票:1回答:2

我正在制作一个应用,并且有一个带有搜索视图的活动。我的意思是我想更改文本字段的背景颜色(screnn镜头中的黑色矩形)。

我的绘画:

<?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>

我的布局:

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

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

有人可以帮我吗?谢谢

java android xml android-activity searchview
2个回答
5
投票

[经过一些侦探工作后,您会看到,如果查看source小部件的SearchView,您将看到它扩展了LinearLayout并增加了一些功能。查看#242行,它包含一个小部件(特别是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 ↑ 

有用的链接:


0
投票

为我工作:

mSearchView.findViewById(android.support.v7.appcompat.R.id.search_plate).setBackground...

也用于语音按钮:

mSearchView.findViewById(android.support.v7.appcompat.R.id.search_voice_btn).setBackground...
© www.soinside.com 2019 - 2024. All rights reserved.