Honeycomb - 自定义操作栏中的SearchView
我有一个字段,用户可以在应用程序的操作栏中键入搜索查询。这是使用活动中的菜单膨胀在操作栏中声明的:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:id="@+id/action_search"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.SearchView"
android:title="@string/search"
></item>
</menu>
我需要自定义 SearchView 的外观(例如背景和文本颜色)。到目前为止,我找不到使用 XML(使用样式或主题)来完成此操作的方法。
膨胀菜单时,我唯一的选择是在代码中执行此操作吗?
编辑#1:我已经尝试过以编程方式,但我无法找到一种简单的方法来设置文本颜色。另外,当我执行 searchView.setBackgroundResource(...) 时,背景是在全局小部件上设置的(当 SearchView 被图标化时也是如此)。
编辑 #2:搜索开发人员参考 要么
I have a field where the user can type a search query in the action bar of the application. This is declared in the action bar using a menu inflate in the Activity:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:id="@+id/action_search"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.SearchView"
android:title="@string/search"
></item>
</menu>
I need to customize the appearance of the SearchView (for instance background and text color). So far I could not find a way to do it using XML (using styles or themes).
Is my only option to do it in the code when inflating the menu?
Edit #1: I have tried programmatically but I cannot get a simple way to set the text color. Plus when I do searchView.setBackgroundResource(...) The background is set on the global widget, (also when the SearchView is iconified).
Edit #2: Not much information on the Search Developer Reference either
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您想更改图标,Seibelj 有一个很好的答案。但你需要
对每个 API 版本都执行此操作。我将 ICS 与 ActionBarSherlock 结合使用,它对我来说并不公平,但它确实将我推向了正确的方向。
下面我更改了文本颜色和提示颜色。我向你展示了如何改变
图标也是如此,尽管我现在对此不感兴趣(而且您可能想使用默认图标以保持一致)
Seibelj had an answer that is good if you want to change the icons. But you'll need to
do it for every API version. I was using ICS with ActionBarSherlock and it didn't do justice for me but it did push me in the correct direction.
Below I change the text color and hint color. I showed how you might go about changing the
icons too, though I have no interest in that for now (and you probably want to use the default icons anyways to be consistent)
添加我对跨不同 Android 版本可能更高效、更安全的事情的看法。
实际上,您可以从字符串 ID 名称获取数字 ID 值。使用android的
hierarchyviewer
工具,你实际上可以找到你感兴趣的事物的字符串ID,然后只需使用findViewById(...)
来查找它们。下面的代码设置编辑字段本身的提示和文本颜色。您可以将相同的模式应用于您想要设计的其他方面。
adding my take on things which is probably a little more efficient and safe across different android versions.
you can actually get a numeric ID value from a string ID name. using android's
hierarchyviewer
tool, you can actually find the string IDs of the things you are interested in, and then just usefindViewById(...)
to look them up.the code below sets the hint and text color for the edit field itself. you could apply the same pattern for other aspects that you wish to style.
您可以使用属性
android:actionLayout
来代替,它可以让您指定要膨胀的布局。只需使用SearchView
进行布局,您就不必真正修改任何内容。至于更改
SearchView
上的文本样式,这可能是不可能的,因为SearchView
是一个ViewGroup
。您可能应该尝试通过主题更改文本颜色。You can use the attribute
android:actionLayout
instead which lets you specify a layout to be inflated. Just have a layout with yourSearchView
and you won't have to modify anything really.As to changing text style on the
SearchView
that is probably not possible as theSearchView
is aViewGroup
. You should probably try changing text color via themes instead.如果有人想直接修改视图,您可以通过以下方法更改颜色/字体/图像并根据自己的喜好自定义搜索框。它被包装在 try/catch 中,以防版本或发行版之间存在差异,因此如果失败,应用程序不会崩溃。
此示例显示更改取消/接受图像的文本颜色和背景颜色。 searchView 是一个已经用它的背景颜色实例化的 SearchView 对象:
这是可绘制代码:
显然,这是 hacky,但它现在可以工作。
In case anyone wants to modify the views directly, here is how you can change the colors/fonts/images and customize the search box to your pleasure. It is wrapped in a try/catch in case there are differences between versions or distributions, so it won't crash the app if this fails.
This example shows changing the text color and the background colors of the cancel/accept images. searchView is a SearchView object already instantiated with it's background color:
Here is the drawable code:
Obviously, this is hacky, but it will work for now.
在 ICS 中,这可以使用主题和样式来实现。我正在使用 ActionBarSherlock,这使得它也适用于 HC 及以下版本。
添加样式来定义“android:textColorHint”:
将其作为“actionBarWidgetTheme”应用到您的主题:
Presto!如果启动了任何可能有其他主题生效的小部件,请确保使用
getSupportActionBar().getThemedContext()
(或 ActionBarSherlock 的getSupportActionBar()
)。急From ICS this is doable using themes and styles. I'm using ActionBarSherlock which makes it applicable also for HC and below.
Add a style to define "android:textColorHint":
Apply this as "actionBarWidgetTheme" to your theme:
Presto! Make sure that you use
getSupportActionBar().getThemedContext()
(orgetSupportActionBar()
for ActionBarSherlock) if any widgets are initiated where you might have other themes in effect.如何在 Activity 中扩充菜单 xml?如果您在 Activity 中使用 getMenuInflator() 来扩充菜单,则菜单和 searchView 都会获得已附加到 Activity 的主题上下文。
如果你检查 API-15 的 Activity.getMenuInflator() 的源代码,你可以看到主题上下文代码。这里是。
How do you inflate the menu xml in your Activity? if you inflate the menu by using getMenuInflator() in your Activity, then the menu and also the searchView get the themed context, that have attached to the activity.
if you check the source code of Activity.getMenuInflator() at API-15, you can see the themed context codes. Here it is.