Android - SearchView - setTypeface不可用

时间:2016-10-10 15:00:32

标签: android fonts searchview

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

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"
        >

3 个答案:

答案 0 :(得分:2)

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,请参阅https://stackoverflow.com/a/30915643/3233251

答案 1 :(得分:1)

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

答案 2 :(得分:0)

通过xml的可能性(未经操作系统版本和设备测试):

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

在search_field.xml内部,在“ ””元素上设置字体家族。

String mask = accountNumber.replaceAll("\\w(?=\\w{4})", "*");

创建资源样式...命名为 my_search_view_style

...
<view class="androidx.appcompat.widget.SearchView$SearchAutoComplete"
              android:id="@+id/search_src_text"
              ...
              app:fontFamily="@font/[YOUR_FONT_RESOURCE]
              />
...

最后在SearchView上设置 my_search_view_style 样式

<style name="my_search_view_style" parent="Widget.AppCompat.SearchView">
   <item name="layout">@layout/search_field</item>
</style>here