2016-10-10 69 views
0

出于某种原因,我无法在SearchView上设置字体。 这里是我的代码,试图改变字体:Android - SearchView - setTypeface不可用

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

回答

1

的搜索查看包含一个TextView,你首先要找到像这样改变它的字体。

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

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

对于在android.widget包正常搜索查看看https://stackoverflow.com/a/30915643/3233251

+0

工作绝对好多了:)谢谢! – Isabelle

1

的TextView SEARCHTEXT =(TextView的)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text); Typeface type = Typeface.createFromAsset(getAssets(),“fonts/siemreap.ttf”); searchText.setTypeface(type);

+1

试着稍微解释一下,让他们知道你的建议。给它上下文。 – Nightforce2