Android - SearchView - setTypeface 不可用

Android - SearchView - setTypeface not available

出于某种原因,我无法在 SearchView 上设置字体。 这是我尝试更改字体的代码:

searchView = findViewById(R.id.searchview_invite_friends_search_contact);
Typeface myFont = Typeface.createFromAsset(getAssets(),"fonts/myFont.ttf");
searchView.setTypeface(myFont);

我在 xml:

中这样定义的
<SearchView
        android:id="@+id/searchView"
        android:layout_width="match_parent"
        android:queryHint="@string/searchview_invite_friends_search_contact"
        android:iconifiedByDefault="false"
        android:layout_centerHorizontal="true"
        android:background="@android:color/white"
        android:searchIcon="@drawable/icon_search"
        android:focusable="false"
        android:layout_height="45dp"
        android:queryBackground="@color/transparent"
        android:gravity="center_horizontal"
        >

SearchView 包含一个 TextView,您首先必须找到它才能像这样更改其字体。

 TextView searchText = (TextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
 searchText.setTypeface(<your Typeface>);

这是针对 v7 Android 支持库中的 SearchView。

对于 android.widget 包中的普通 SearchView,请参阅

TextView searchText = (TextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text); 字体类型 = Typeface.createFromAsset(getAssets(),"fonts/siemreap.ttf"); searchText.setTypeface(类型);

通过 xml 的可能性(未在 OS 版本和设备上测试):

创建与@layout/abc_search_view相同的资源。让我们将其命名为“search_field.xml”

在 search_field.xml 中,在“