android中的SearchView自定义字体

android中的SearchView自定义字体,android,fonts,searchview,android-typeface,Android,Fonts,Searchview,Android Typeface,我一直在尝试将自定义字体设置为android.support.v7.widget.SearchView查询提示和在视图中输入的文本。我确实尝试通过创建一个TypeFace对象将字体从Assets动态设置为SearchView,但问题出现在“SearchView不包含setTypeface(TypeFace tf)方法。”我尝试过自定义SearchView类,但找不到 private void setFont(String font1, String font2, String font3)

我一直在尝试将自定义字体设置为
android.support.v7.widget.SearchView
查询提示和在视图中输入的文本。我确实尝试通过创建一个TypeFace对象将字体从Assets动态设置为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);

然后像往常一样设置字体。

要以编程方式从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);

对于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
val-face:Typeface=Typeface.createFromAsset(context?.assets,“font.otf”)
val searchText=searchView.findviewbyd(androidx.appcompat.R.id.search\u src\u text)作为文本视图
searchText.typeface=face

感谢前面的所有答案,我找到了最好的解决方案,希望您也能找到最干净的答案

这适用于包android.widget中的SearchView

首先,创建一个扩展,如下所示:

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
}
然后在您喜欢的任何地方使用此扩展:

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

您可能希望通过资产或其他方式获得字体。但是其余的都一样。

试试这个
searchView.setQueryHint(Html.fromHtml(“+getResources().getString(R.string.search\u hint)+”)@Apurva让我检查一下,然后告诉你know@Apurva不,这不起作用。虽然我认为这是可行的,但您假设的是SearchView类的实现细节。这些可能随时改变,这不是一个安全的假设。对于androidX,要搜索的ID是androidX.appcompat.R.ID.search\u src_text@amitav13如果我使用
AndroidX.appcompat.R.id.search\u src\u text
作为id,我会在AndroidX中得到一个空的
TextView
。但是,第二个方法使用
getIdentifier()
完全按照建议对我有效。我不必将android:id/search\u src\u text
更改为
androidx.appcompat.R.id.search\u src\u text
。你知道为什么androidx.appcompat.R.id.search\u src\u text不起作用吗?