简体   繁体   English

android中的SearchView自定义字体

[英]SearchView custom font in android

I have been trying to set custom font to the android.support.v7.widget.SearchView query hint and the text entered in the View.I did try setting the font dynamically from the assests to the searchView by creating a TypeFace object, but the problem occurs that "SearchView doesn't contain a setTypeface(Typeface tf) method."我一直在试图设置自定义字体到android.support.v7.widget.SearchView查询提示,并在View.I进入并通过创建一个字体对象从资产产生的搜索查看动态设置字体文本,但出现“SearchView 不包含 setTypeface(Typeface tf) 方法”的问题。 I did try for a custom SearchView class but couldn't find one.我确实尝试过自定义 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..

    }

The workaround here is to first get the searchView's textView and then change the typeface for the textView instead:这里的解决方法是首先获取 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);

Or, if you're not using appcompatv7:或者,如果您没有使用 appcompatv7:

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

then set the typeface as usual.然后像往常一样设置字体。

To change the font family in searchview programmatically from xml font folder:从 xml 字体文件夹以编程方式更改 searchview 中的字体系列:

Typeface tf = ResourcesCompat.getFont(dis,R.font.montserrat_regular);
TextView searchText = (TextView)search_view.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchText.setTypeface(tf);

For Kotlin and Androidx对于 Kotlin 和 Androidx

val face: Typeface = Typeface.createFromAsset(context?.assets, "font.otf")
val searchText = searchView.findViewById<View>(androidx.appcompat.R.id.search_src_text) as TextView
searchText.typeface = face

Thanks to all the previous answers, I found my best solution witch I hope that you find it the cleanest answer, too.感谢之前的所有答案,我找到了我最好的解决方案女巫,我希望你也能找到最干净的答案。

This is working for SearchView class inside the package android.widget这适用于包android.widget 中的SearchView

First, create an extension like below:首先,创建一个扩展,如下所示:

import android.graphics.Typeface
import android.widget.SearchView
import android.widget.TextView

fun SearchView.setTypeFace(typeface: Typeface?) {
    val id = context.resources.getIdentifier("android:id/search_src_text", null, null)
    val searchText = searchView.findViewById(id) as TextView
    searchText.typeface = typeface
}

Then use this extension wherever you like:然后在任何你喜欢的地方使用这个扩展:

val typeface = ResourcesCompat.getFont(requireContext(), R.font.sans)
searchView.setTypeFace(typeface)

You may want to get typeface from assets or other ways.您可能希望从资产或其他方式获取字体。 but the rest is the same.但其余的都是一样的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM