Android - 如何构建一个自定义 TextView ,该 TextView 的上标与美元值(value)对齐

标签 android spannable

我想构建一个可以像这样显示钱的自定义 TextView :

enter image description here

最后我希望任何用户都可以这样调用它:

<com.util.MyCustomViews.MoneyTextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="24.92"
    android:textColor="#727272"
    android:textSize="18dp" />

我遇到的两个问题是,如果我使用 html 来实现它,它并不是用户体验团队想要筹集的资金的确切高度。正如您在照片中看到的那样,他们只希望将美分提高到一半。所以叫<p>This text contains <sup>superscript</sup>45</p>把它抬得太高。我需要自己养。我不确定如何开始,但我假设我会像这样在自定义 textView 中超越 setText:

class MoneyTextView extends AppCompatTextView {
    public MoneyTextView(Context context) {
        super(context);
    }

    public MoneyTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MoneyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        Spannable s = getSomeCustomSpannableSuperScriptString(getContext(), text);
        super.setText(s, BufferType.SPANNABLE);
    }
}

更新:我试过下面的代码,但没有用:

公共(public)类 MoneyTextView 扩展了 AppCompatTextView {

static final String SEPERATOR = ".";
static final double HEIGHT_IN_EM = 0.5;
static final int MOGO_BASELINE_SHIFT = 10;

public MoneyTextView(Context context) {
    super(context);
}

public MoneyTextView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
}

public MoneyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
public void setText(CharSequence text, BufferType type) {

    super.setText(Html.fromHtml("<b>$27</b><sup style=\"vertical-align:top,line-height:"+HEIGHT_IN_EM+"em\">.45</sup>"), BufferType.SPANNABLE);
}

并将其与此常量一起使用:static final double HEIGHT_IN_EM = 0.5;

但这是我在 nexus api 19 模拟器上得到的输出,无论我选择的高度如何:

enter image description here

它不会压低“.45”。

如果需要,布局文件本身如下所示:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.myapplicationtest.MainActivity">

    <com.example.myapplicationtest.MoneyTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|center_vertical"
        android:text="666.33"
        android:textSize="22dp"/>

</LinearLayout>

最佳答案

看看Ticker .您可以根据需要更改来源。

enter image description here

关于Android - 如何构建一个自定义 TextView ,该 TextView 的上标与美元值(value)对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43199605/

相关文章:

java - 基于 JSF 的触摸网站框架

android - 具有 ImageView 的缩放/平移框架布局

android - 如何设置 spannable textview 之间的边距?

java - 如何以编程方式添加底部填充以跨越RelativeLayout中textView的内容

android - SpannableString:是否可以应用两个或多个RelativeSizeSpans?

android - 你好,世界和 Android 模拟器没有一路启动!

armeabi-v7a : "PLT offset too large, try linking with --long-plt" 的 Android NDK 链接器失败

android - Volley - 直接下载到文件(没有内存字节数组)

java - 在 android 上可跨 textView

java - 如何在Android中显示不同大小的文字,例如Office或Google Docs的功能