通常,当我们在文本之前给出*时,我看起来像(* hai)但是当在文本视图的字符串文件中使用相同的文本时,它看起来不一样。我想在textview的左上角设置*。
普通的setText(" *清洁费适用");不会把星号放在我想要的地方。
我希望输出像附加屏幕截图。请给我解决方案。
答案 0 :(得分:1)
我用这个类来创建一个上标:
import android.text.TextPaint;
import android.text.style.MetricAffectingSpan;
/**
* A Helper class to adjust the alignment in a textview or other text section,
* used when SpannableStrings sets text dynamically.
*
*/
public class SuperscriptSpanAdjuster extends MetricAffectingSpan {
double ratio = 0.5;
public SuperscriptSpanAdjuster(double ratio) {
this.ratio = ratio;
}
@Override
public void updateMeasureState(TextPaint p) {
p.baselineShift += (int) (p.ascent() * ratio);
}
@Override
public void updateDrawState(TextPaint arg0) {
arg0.baselineShift += (int) (arg0.ascent() * ratio);
}
}
然后像这样使用它:
String temp = getString("*Cleaning Fee Applies");
SpannableString s = new SpannableString(temp);
s.setSpan(new SuperscriptSpanAdjuster(3.0/5.0), 0, 1, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
((TextView) this.findViewById(R.id.mytextView)).setText(s);
答案 1 :(得分:0)
尝试以下代码:
text.setTransformationMethod(new AsteriskPasswordTransformationMethod());
public class AsteriskPasswordTransformationMethod extends PasswordTransformationMethod {
@Override
public CharSequence getTransformation(CharSequence source, View view) {
return new PasswordCharSequence(source);
}
private class PasswordCharSequence implements CharSequence {
private CharSequence mSource;
public PasswordCharSequence(CharSequence source) {
mSource = source; // Store char sequence
}
public char charAt(int index) {
return '*'; // This is the important part
}
public int length() {
return mSource.length(); // Return default
}
public CharSequence subSequence(int start, int end) {
return mSource.subSequence(start, end); // Return default
}
}
};