我正在制作一个应用程序,并且我有一个带有搜索 View 的 Activity 。我的意思是我想更改文本字段的背景颜色(屏幕截图中的黑色矩形)。
我的可绘制对象:
<?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
有人可以帮我吗? 谢谢
最佳答案
经过一些侦探工作后,如果您查看 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 ↑
有用的链接:
关于java - 在android sdk的搜索 View 中更改文本字段的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21798683/