2016-02-08 45 views
5

是否可以在选择器中使用字体图标而不是可绘制?如何在XML选择器中使用字体图标(font-awesome)

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

回答

4

我在选择器中改变了文本颜色而不是绘制。 它的工作正常。

创建延伸的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); 
    } 
} 

MyTextView类创建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> 
1

您可以使用字体真棒图标如下:

1 - 复制字体真棒字体文件到您的资产目录

2 - 发现了字符实体图标我想,用this页面

3 - 在strings.xml中为每个图标创建条目。例如:

<string name="icon_eg">&#xf13d;</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" /> 

检查this链接了解更多信息。

您不能将其用作选择器。但您可以动态更改图标。

+0

感谢您的答案,但我想用选择器代替简单的图标 –

+0

,因为它是字体(文本),所以这是不可能的。 – droidev

1

最简单的办法!

由于Android支持库26和Android Studio 3.0可以使用XML中的原生字体支持。

https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html

  1. 创建res目录字体目录
  2. 复制字体文件到字体目录
  3. 创建不久的字体文件,新的字体资源文件

    <?xml version="1.0" encoding="utf-8"?> 
    <font-family xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:app="http://schemas.android.com/apk/res-auto"> 
        <font 
         android:font="@font/font_awesome_font" 
         android:fontStyle="normal" 
         android:fontWeight="400" 
         app:font="@font/font_awesome_font" 
         app:fontStyle="normal" 
         app:fontWeight="400" /> 
    </font-family> 
    

    使用android:font的API 26和自14以来支持API的app:font

  4. 现在你可以在TextView使用fontFamily

    <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:fontFamily="@font/font_awesome" 
        android:text="\uF0da" /> 
    
  5. 要与\u

注意插入字体真棒字符输入它UTF代码: Android的工作室设计预览没有按” t显示字体真棒字符,但在应用程序中它正常工作。

相关问题