首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在XML选择器中使用字体图标(字体-可怕)

如何在XML选择器中使用字体图标(字体-可怕)
EN

Stack Overflow用户
提问于 2016-02-08 14:07:08
回答 3查看 6.9K关注 0票数 6

是否可以在选择器中使用字体图标而不是绘图?

代码语言:javascript
运行
复制
<?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>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-02-09 07:13:02

我在选择器中更改了文本颜色,而不是可绘制的。工作很好。

创建扩展MyTextView的TextView类

代码语言:javascript
运行
复制
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选择器

代码语言:javascript
运行
复制
<?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>

然后在你的布局中使用它

代码语言:javascript
运行
复制
 <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>
票数 4
EN

Stack Overflow用户

发布于 2017-10-28 11:28:00

是最简单的方法!

由于26和AndroidStudio3.0,您可以在XML中使用本机字体支持。

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

  1. 在res目录中创建字体目录
  2. 将字体文件复制到字体目录中
  3. 创建字体附近的新字体资源文件fileUse android:font for API 26,app:font用于支持API 14。
  4. 现在您可以在fontFamily中使用TextView: “
  5. 要插入字体-可怕的字符,请使用\u输入它的UTF代码。

注意: Android的设计预览不显示字体-令人敬畏的字符,但在应用程序中,它工作正常。

票数 3
EN

Stack Overflow用户

发布于 2016-02-08 14:15:50

您可以使用字体-令人敬畏的图标如下:

将字体-令人敬畏的字体文件复制到资产目录

2-使用页面为我想要的图标找到字符实体

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

代码语言:javascript
运行
复制
<string name="icon_eg">&#xf13d;</string>

4-在onCreate方法中加载字体并将其设置为适当的视图:

代码语言:javascript
运行
复制
Typeface font = Typeface.createFromAsset( getAssets(), "fontawesome-webfont.ttf" );
...
Button button = (Button)findViewById( R.id.like );
button.setTypeface(font);

不要忘记引用视图中的字符串。

代码语言:javascript
运行
复制
<Button
     android:id="@+id/my_btn"
     style="?android:attr/buttonStyleSmall"
     ...
     android:text="@string/icon_eg" />

有关更多信息,请查看链接。

你不能用它作为选择器。但是您可以动态地更改图标。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35271608

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档