Java 更改android sdk搜索视图中文本字段的背景
我正在制作一个应用程序,我有一个带有searchview的活动。我的观点是,我想更改文本字段的背景色(screnn快照中的黑色矩形) 我的提款权:Java 更改android sdk搜索视图中文本字段的背景,java,android,xml,android-activity,searchview,Java,Android,Xml,Android Activity,Searchview,我正在制作一个应用程序,我有一个带有searchview的活动。我的观点是,我想更改文本字段的背景色(screnn快照中的黑色矩形) 我的提款权: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="1
<?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>
截图:
有人能帮我吗?
谢谢在做了一些检测工作之后,您将看到,如果您查看
搜索视图
小部件,您将看到它扩展了线性布局
,并添加了一些功能。查看行#242
它包含一个小部件(特别是SearchAutoComplete
,它扩展了AutoCompleteTextView
,最后扩展了EditText
话虽如此,您需要修改该子元素的background属性,并在以下行中添加一些内容:
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 ↑
有用链接:
搜索视图
小部件,您会看到它扩展了线性布局
,并添加了一些功能。查看该行242
它包含一个小部件(特别是一个SearchAutoComplete
,它扩展了AutoCompleteTextView
,最后扩展了EditText
话虽如此,您需要修改该子元素的background属性,并在以下行中添加一些内容:
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 ↑
有用链接:
- 对我有效:
mSearchView.findViewById(android.support.v7.appcompat.R.id.search_plate).setBackground...
也适用于语音按钮:
mSearchView.findViewById(android.support.v7.appcompat.R.id.search_voice_btn).setBackground...
为我工作:
mSearchView.findViewById(android.support.v7.appcompat.R.id.search_plate).setBackground...
也适用于语音按钮:
mSearchView.findViewById(android.support.v7.appcompat.R.id.search_voice_btn).setBackground...
粘贴绘图表(如果不是XML,则链接到它们)您在pastebin链接中有drawable…在底部,在活动XmlXML之后,XML中的一个可能是用于背景的,但肯定不是@drawable/searchviewJuan,pastebin中的第二个是drawable文件夹中的searchview.XML。为什么您不这么认为?我在这里添加了代码…粘贴drawable(如果不是XML,则链接到它们)你在pastebin链接中有drawable…在底部,在活动XmlXML之后,XML中的一个可能是用于背景的,但肯定不是@drawable/searchviewJuan,pastebin中的第二个是drawable文件夹中的searchview.XML。为什么你不这么认为?我在这里添加了代码…谢谢。我还在尝试。我得到了“EditTex”t无法解析为类型。我必须扩展类?这必须用java完成?我得到了“c无法解析为类型”。我必须正确导入某些内容?你知道是什么吗?很抱歉,我将变量重命名为c和e以获得简短的代码,请尝试现在创建一个具有所需边界的可绘制文件(无论是XML还是9patch可绘制文件)把挫折称为↑ 代码谢谢。我知道了。但是我在searchview的edittext周围还有一条线,不是边界。我不知道这是什么。你知道吗?谢谢。我还在尝试。我得到了“edittext不能解析为类型”。我必须扩展类?这必须用java完成?我得到了“c不能解析为类型”。我必须导入一些东西,对吗你知道这是什么吗?很抱歉,我将变量重命名为c和e以获得短代码,请现在尝试创建一个具有所需边框的绘图表(可以是XML或9patch绘图表),并像中一样调用setBackground↑ 代码谢谢。我知道了。但我在searchview的edittext周围还有一条线,不是边界。我不知道这是什么……你知道吗?