【问题标题】:SearchView custom font in androidandroid中的SearchView自定义字体
【发布时间】:2015-06-08 06:11:33
【问题描述】:

我一直在尝试将自定义字体设置为 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.setQueryHint(Html.fromHtml("<font face = tf>" + getResources().getString(R.string.search_hint) + "</font>"));
  • @Apurva 让我检查并告诉你
  • @Apurva 不,这不起作用..

标签: android fonts searchview android-typeface


【解决方案1】:

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

这适用于 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)

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

【讨论】:

    【解决方案2】:

    对于 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
    

    【讨论】:

      【解决方案3】:

      从 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);
      

      【讨论】:

        【解决方案4】:

        这里的解决方法是先获取 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);
        

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

        【讨论】:

        • 虽然我假设这可行,但您假设 SearchView 类的实现细节。这些可能随时更改,这不是一个安全的假设。
        • 对于androidX,要搜索的ID是androidx.appcompat.R.id.search_src_text
        • @amitav13 如果我使用 androidx.appcompat.R.id.search_src_text 作为 ID,我会在 AndroidX 中得到一个空的 TextView。但是,使用getIdentifier() 的第二种方法完全符合我的建议。我不必将android:id/search_src_text 更改为androidx.appcompat.R.id.search_src_text。任何想法为什么 androidx.appcompat.R.id.search_src_text 不起作用?
        猜你喜欢
        • 2020-04-21
        • 2017-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-22
        • 2011-04-12
        • 2015-12-29
        相关资源
        最近更新 更多