是否可以在选择器中使用字体图标而不是绘图?
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/menu_home_press" android:state_pressed="true"></item>
<item android:drawable="@drawable/menu_home"></item>
</selector>发布于 2016-02-09 07:13:02
我在选择器中更改了文本颜色,而不是可绘制的。工作很好。
创建扩展MyTextView的TextView类
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public MyTextView(Context context) {
super(context);
init(context);
}
private void init(Context context) {
Typeface tf = Typeface.createFromAsset(context.getAssets(),
"fontawesome-webfont.ttf");
setTypeface(tf);
}
}创建text_color_selector.xml选择器
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#ff0000" android:state_pressed="true" />
<!-- pressed -->
<item android:color="#ff0000" android:state_focused="true" />
<!-- focused -->
<item android:color="#000000" />
<!-- default -->
</selector>然后在你的布局中使用它
<com.example.mohsin.myapplication.MyTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="50sp"
android:clickable="true"
android:textColor="@drawable/text_color_selector"
android:text="\uF242">
</com.example.mohsin.myapplication.MyTextView>发布于 2017-10-28 11:28:00
是最简单的方法!
由于26和AndroidStudio3.0,您可以在XML中使用本机字体支持。
https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html
android:font for API 26,app:font用于支持API 14。fontFamily中使用TextView:
“\u输入它的UTF代码。注意: Android的设计预览不显示字体-令人敬畏的字符,但在应用程序中,它工作正常。
发布于 2016-02-08 14:15:50
您可以使用字体-令人敬畏的图标如下:
将字体-令人敬畏的字体文件复制到资产目录
2-使用这页面为我想要的图标找到字符实体
3-在strings.xml中为每个图标创建条目。例:
<string name="icon_eg"></string>4-在onCreate方法中加载字体并将其设置为适当的视图:
Typeface font = Typeface.createFromAsset( getAssets(), "fontawesome-webfont.ttf" );
...
Button button = (Button)findViewById( R.id.like );
button.setTypeface(font);不要忘记引用视图中的字符串。
<Button
android:id="@+id/my_btn"
style="?android:attr/buttonStyleSmall"
...
android:text="@string/icon_eg" />有关更多信息,请查看这链接。
你不能用它作为选择器。但是您可以动态地更改图标。
https://stackoverflow.com/questions/35271608
复制相似问题