Java 在Android文本切换器中使用上标
我无法让我的文本切换器支持上标文本。无论我尝试什么,它都显示为常规大小的文本 以下是我尝试过的:Java 在Android文本切换器中使用上标,java,android,xml,eclipse,superscript,Java,Android,Xml,Eclipse,Superscript,我无法让我的文本切换器支持上标文本。无论我尝试什么,它都显示为常规大小的文本 以下是我尝试过的: Spanned span = Html.fromHtml("<sup>TM</sup>"); String subscript = span.toString(); mSwitcher.setText(getText(R.string.desc_about1) + subscript); span=Html.fromHtml(
Spanned span = Html.fromHtml("<sup>TM</sup>");
String subscript = span.toString();
mSwitcher.setText(getText(R.string.desc_about1) + subscript);
span=Html.fromHtml(“TM”);
字符串下标=span.toString();
mSwitcher.setText(getText(R.string.desc_about1)+下标);
然后我尝试在XML字符串文件中设置它:
<string name="desc_about1">Android<sup><small>TM</small></sup></string>
AndroidTM
这两种方法都行不通。还有别的办法吗
我的文本切换器代码供参考:
<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;
}
@凌驾
公共视图makeView(){
TextView t=新的TextView(此);
t、 设置重力(Gravity.TOP | Gravity.LEFT);
t、 setTextSize(18);
返回t;
}
谢谢大家 您的第一个示例对我来说似乎不正确,请尝试以下方法:
Spanned span = Html.fromHtml(getText(R.string.desc_about1) + "<sup>TM</sup>");
mSwitcher.setText(span);
span=Html.fromHtml(getText(R.string.desc_about1)+“TM”);
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;
}
package com.stackoverflow;
导入android.app.Activity;
导入android.os.Bundle;
导入android.text.Html;
导入android.view.Gravity;
导入android.view.view;
导入android.widget.TextSwitcher;
导入android.widget.TextView;
导入android.widget.ViewSwitcher.ViewFactory;
公共类测试扩展活动实现ViewFactory{
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextSwitcher ts=(TextSwitcher)findviewbyd(R.id.switcher);
ts.setFactory(本);
ts.setText(Html.fromHtml(“TestTM”));
}
@凌驾
公共视图makeView(){
TextView t=新的TextView(此);
t、 设置重力(Gravity.TOP | Gravity.LEFT);
t、 setTextSize(18);
返回t;
}
}
如果要使用strings.xml存储文本:
mSwitcher.setText(Html.fromHtml(getText(R.string.desc_about1) + "<sup>TM</sup>"));
mSwitcher.setText(Html.fromHtml(getText(R.string.desc_about1)+“TM”);
只需确保传递到setText的最新内容是来自HTML输出。我知道前一个条目在2年前是一个可接受的答案,但我添加了一个稍微不同的解决方案
Spanned span = Html.fromHtml(getText(R.string.desc_about1) + "™");
mSwitcher.setText(span);
贸易部;从字符集中转换为单个字符,而不是像中那样转换为更小的超级脚本TM™ 奇怪。。我刚刚试过,它对我有效。确实要将Html.fromHtml()输出直接传递给setText方法吗?在你的例子中,你把它转换成了字符串,这就是问题所在。是的,我刚刚测试过。我不得不做TM,它成功了。不幸的是,如果我这样做的话,那么我就不能使用string.xml文件,因为我不能在一个跨区上使用“+”操作符。唉……权衡。谢谢您可以无限制地使用strings.xml。请参阅更新的示例。