受@ Nowshad的回答启发,我可以在没有带有数据绑定库的外部库的情况下做到这一点.
必要时在布局文件中提供货币和金额:
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
app:amount="@{amount}"
app:currency="@{currency}" />
并提供绑定适配器:
@BindingAdapter({"currency", "amount"})
public static void setCurrencyAndAmount(TextView textView, String currency, double amount) {
SpannableString spannableString = new SpannableString(currency + " " +
new DecimalFormat("#.##").format(amount));
spannableString.setSpan(new RelativeSizeSpan(0.6f), 0, 1, 0);
textView.setText(spannableString);
}
简单高效.