【问题标题】:How to set SerachView background white and text Color black如何设置 SearchView 背景白色和文本颜色黑色
【发布时间】:2017-09-04 13:03:40
【问题描述】:

我找不到任何关于 Xamarin android 处理 SearchView 的文章。

下面的 searchView 将提供黑色背景和白色 textColor

1) 我需要将背景颜色设置为白色和 textColor 白色。
2) 如果用户决定不使用搜索,键盘不会消失。点击 searchView 后如何关闭键盘?

谢谢

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical">
    <SearchView
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:id="@+id/sv" />
    <ListView
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/merchantListView" />
</LinearLayout>

【问题讨论】:

    标签: xamarin xamarin.android


    【解决方案1】:

    使用以下代码将文本颜色设置为黑色

    SearchView sv = FindViewById<SearchView>(Resource.Id.sv);
    int id = sv.Context.Resources.GetIdentifier("android:id/search_src_text", null, null);
    TextView tv = (TextView)sv.FindViewById(id);
    tv.SetTextColor(Android.Graphics.Color.Black);
    

    您可以单击键盘中的搜索玻璃图标将其关闭,或者在需要关闭 SearchView 焦点时设置sv.ClearFoucs()

    【讨论】:

    • 好代码。我又添加了一行 tv.SetBackgroundColor((Android.Graphics.Color.ParseColor("#ffffff"))); tv.SetTextColor(Android.Graphics.Color.Black);如何确定 search_src_text 是 SerachView 的 ID?如何通过单击屏幕上的任意位置来关闭它?
    • 现在我知道我可以点击返回键来关闭键盘。至于 UX 考虑,用户可能希望点击屏幕上的任意位置以关闭键盘。我正在寻找这个解决方案。谢谢
    • 嗨 Isham 我按照您的建议添加了代码,但是电视为空。我将代码放在 OnCreateOptionsMenu 中。是这个地方吗?
    • @Yaza 你的安卓版本是多少?
    • @IshamMohamed 我的最低安卓版本是 4.1 API 级别 16。
    猜你喜欢
    • 1970-01-01
    • 2021-12-03
    • 2010-11-22
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-20
    相关资源
    最近更新 更多