Android工具集

DataBind第五篇:引入其他的类到xml中

2019-06-24  本文已影响1人  Small_Cake

原文链接:https://www.jianshu.com/p/05462a4dd927

有的时候我们需要在代码中对设置的字符进行处理,比如变红放大等,在xml中引入相关的类型其实也是可以的。

1.引入:引入相关的工具类到xml中
<import type="cn.com.smallcake_utils.SpannableStringUtils" alias="spanUtils"/>
2.使用:
android:text='@{spanUtils.getBuilder(item.title).append("\n"+item.title).setForegroundColor(@color/red).setProportion(1.5f).create()}'

完整的xml文件

<?xml version="1.0" encoding="utf-8"?>
<layout>
    <data>
        <import
            alias="spanUtils"
            type="cn.com.smallcake_utils.SpannableStringUtils" />
        <variable
            name="item"
            type="com.smallcake.model.MenuItem" />
    </data>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text='@{spanUtils.getBuilder(item.title).append("\n"+item.title).setForegroundColor(@color/red).setProportion(1.5f).create()}'
            android:textSize="40sp" />
    </LinearLayout>
</layout>

最后的效果为:


字体变红,放大1.5
上一篇下一篇

猜你喜欢

热点阅读