在 Android textSwitcher 中使用上标

发布于 2024-09-29 17:25:47 字数 841 浏览 6 评论 0原文

我无法让我的 textSwitcher 支持上标文本。无论我尝试什么,它都会显示为常规大小的文本。

这是我尝试过的:

        Spanned span = Html.fromHtml("<sup>TM</sup>");
        String subscript = span.toString();
        mSwitcher.setText(getText(R.string.desc_about1) + subscript);

然后我尝试将其设置在 XML 字符串文件中:

<string name="desc_about1">Android<sup><small>TM</small></sup></string>

这些都不起作用。还有别的办法吗?

我的textSwitcher代码供参考:

    <TextSwitcher android:id="@+id/switcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


@Override
public View makeView() {
    TextView t = new TextView(this);
    t.setGravity(Gravity.TOP | Gravity.LEFT);
    t.setTextSize(18);
    return t;
}

谢谢大家!

I can't manage to get my textSwitcher to be able to support superscript text. No matter what I try it shows up as regular size text.

Here is what I've tried:

        Spanned span = Html.fromHtml("<sup>TM</sup>");
        String subscript = span.toString();
        mSwitcher.setText(getText(R.string.desc_about1) + subscript);

Then I tried setting it in the XML string file:

<string name="desc_about1">Android<sup><small>TM</small></sup></string>

Neither one of these will work. Is there another way?

My textSwitcher code for reference:

    <TextSwitcher android:id="@+id/switcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


@Override
public View makeView() {
    TextView t = new TextView(this);
    t.setGravity(Gravity.TOP | Gravity.LEFT);
    t.setTextSize(18);
    return t;
}

Thanks all!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一绘本一梦想 2024-10-06 17:25:47

您的第一个示例对我来说看起来不正确,请尝试以下操作:

    Spanned span = Html.fromHtml(getText(R.string.desc_about1) + "<sup>TM</sup>");
    mSwitcher.setText(span);

对我有用的完整示例:

package com.stackoverflow;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.view.Gravity;
import android.view.View;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;

public class Test extends Activity implements ViewFactory {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextSwitcher ts = (TextSwitcher) findViewById(R.id.switcher);
        ts.setFactory(this);
        ts.setText(Html.fromHtml("Test<sup>TM</sup>"));
    }

    @Override
    public View makeView() {
        TextView t = new TextView(this);
        t.setGravity(Gravity.TOP | Gravity.LEFT);
        t.setTextSize(18);
        return t;
    }

}

如果您想使用 strings.xml 来存储文本:

 mSwitcher.setText(Html.fromHtml(getText(R.string.desc_about1) + "<sup>TM</sup>"));

只需确保您传递到 setText 的最新内容是 fromHtml 输出。

Your first example looks incorrect for me, try this instead:

    Spanned span = Html.fromHtml(getText(R.string.desc_about1) + "<sup>TM</sup>");
    mSwitcher.setText(span);

Full example that works for me:

package com.stackoverflow;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.view.Gravity;
import android.view.View;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;

public class Test extends Activity implements ViewFactory {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextSwitcher ts = (TextSwitcher) findViewById(R.id.switcher);
        ts.setFactory(this);
        ts.setText(Html.fromHtml("Test<sup>TM</sup>"));
    }

    @Override
    public View makeView() {
        TextView t = new TextView(this);
        t.setGravity(Gravity.TOP | Gravity.LEFT);
        t.setTextSize(18);
        return t;
    }

}

And if you want to use strings.xml to store you texts:

 mSwitcher.setText(Html.fromHtml(getText(R.string.desc_about1) + "<sup>TM</sup>"));

Just make sure that latest thing you pass into setText is fromHtml output.

梦醒时光 2024-10-06 17:25:47

我知道上一个条目是两年多前被接受的答案,但我添加了一个略有不同的解决

Spanned span = Html.fromHtml(getText(R.string.desc_about1) + "™");
mSwitcher.setText(span);

方案贸易;转换为字符集中的单个字符,而不是像 ™ 中那样更小的上标 TM

I know that the previous entry was an accepted answer just over 2 years ago, but I'm adding a slightly different solution

Spanned span = Html.fromHtml(getText(R.string.desc_about1) + "™");
mSwitcher.setText(span);

The & trade; converts to a single character from the character set rather than a smaller and super-script TM as in ™

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文