android - android中的SearchView自定义字体

标签 android fonts searchview android-typeface

我一直在尝试将自定义字体设置为 android.support.v7.widget.SearchView 查询提示和在 View 中输入的文本。我确实尝试从 通过创建 TypeFace 对象评估到 searchView,但出现的问题是 “SearchView 不包含 setTypeface(Typeface tf) 方法。” 我确实尝试了自定义 SearchView 类但找不到。

 private void setFont(String font1, String font2, String font3)
 {
        Typeface tf = null;

        tf = UiUtil.changeFont(MainActivity.this, font1);
        btn1.setTypeface(tf);

        tf = UiUtil.changeFont(MainActivity.this, font2);
        btn2.setTypeface(tf);

        tf = UiUtil.changeFont(MainActivity.this, font3);
        btn3.setTypeface(tf);

         tf = UiUtil.changeFont(MainActivity.this, font3);
        // no set typeface method..

    }

最佳答案

这里的解决方法是首先获取 searchView 的 textView,然后改为更改 textView 的字体:

 TextView searchText = (TextView)
 searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
 Typeface myCustomFont = Typeface.createFromAsset(getAssets(),"fonts/myFont.ttf");
 searchText.setTypeface(myCustomFont);

或者,如果您没有使用 appcompatv7:

 int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
 TextView searchText = (TextView) searchView.findViewById(id);

然后照常设置字体。

关于android - android中的SearchView自定义字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29427350/

相关文章:

html - QuickSand(谷歌字体)在 Chrome 和 IE 中的字体呈现不佳

java - LIGATURES 在 java/awt/font/TextAttribute 中如何工作?

css - 当字体提供不同的 Italic 和 Oblique 样式时,如何显示它们?

android - 如何像在 Google Play 商店应用中一样设置 Android SearchView 的样式?

AndroidStudio - Gradle 中的模块依赖

android - Facebook SDK中定义的登录请求代码常量(64206)在哪里

android - SearchView 中的文本输入不显示

java - Android searchview 删除提示填充

java - 如何阻止使用 LayoutInflater().inflate(int resource, ViewGroup root, boolean AttachToRoot) 引发 IllegalStateExceptions?

java - 错误 'Can' 无法解析 MainActivity 中的符号 'container'