我正在尋找如何在編輯文本中爲提示和用戶輸入文本設置不同的文本大小。我對此有兩個問題。 1)是否可以爲提示和用戶輸入顯示不同大小的文本? 2)如果是,那我們該怎麼做到? 我指Android EditText Hint Size 鏈接,但任何其他可供選擇像自定義樣式。 請引導我一樣。EditText提示和文本大小不同的文本
0
A
回答
2
使用此代碼..
editText.setHint(Html.fromHtml(
"<font size=\"5\">" + "hinttext1" + "</font>" +
"<small>" + "hinttext2" + "</small>"));
0
使用HTML
不夠靈活。我會建議使用另一種方式。您需要自定義SpannableString
和MetricAffectingSpan
。
1)創建自定義Hint
對象:
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.MetricAffectingSpan;
public class CustomHint extends SpannableString
{
public CustomHint(final CharSequence source, final int style)
{
this(null, source, style, null);
}
public CustomHint(final CharSequence source, final Float size)
{
this(null, source, size);
}
public CustomHint(final CharSequence source, final int style, final Float size)
{
this(null, source, style, size);
}
public CustomHint(final Typeface typeface, final CharSequence source, final int style)
{
this(typeface, source, style, null);
}
public CustomHint(final Typeface typeface, final CharSequence source, final Float size)
{
this(typeface, source, null, size);
}
public CustomHint(final Typeface typeface, final CharSequence source, final Integer style, final Float size)
{
super(source);
MetricAffectingSpan typefaceSpan = new CustomMetricAffectingSpan(typeface, style, size);
setSpan(typefaceSpan, 0, source.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
}
2)創建的自定義對象MetricAffectingSpan
:
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.MetricAffectingSpan;
public class CustomMetricAffectingSpan extends MetricAffectingSpan
{
private final Typeface _typeface;
private final Float _newSize;
private final Integer _newStyle;
public CustomMetricAffectingSpan(Float size)
{
this(null, null, size);
}
public CustomMetricAffectingSpan(Float size, Integer style)
{
this(null, style, size);
}
public CustomMetricAffectingSpan(Typeface type, Integer style, Float size)
{
this._typeface = type;
this._newStyle = style;
this._newSize = size;
}
@Override
public void updateDrawState(TextPaint ds)
{
applyNewSize(ds);
}
@Override
public void updateMeasureState(TextPaint paint)
{
applyNewSize(paint);
}
private void applyNewSize(TextPaint paint)
{
if (this._newStyle != null)
paint.setTypeface(Typeface.create(this._typeface, this._newStyle));
else
paint.setTypeface(this._typeface);
if (this._newSize != null)
paint.setTextSize(this._newSize);
}
}
3)使用:
Typeface newTypeface = Typeface.createFromAsset(getAssets(), "AguafinaScript-Regular.ttf");
CustomHint customHint = new CustomHint(newTypeface, "Enter some text", Typeface.BOLD_ITALIC, 60f);
// CustomHint customHint = new CustomHint(newTypeface, "Enter some text", Typeface.BOLD_ITALIC);
// CustomHint customHint = new CustomHint(newTypeface, "Enter some text", 60f);
// CustomHint customHint = new CustomHint("Enter some text", Typeface.BOLD_ITALIC, 60f);
// CustomHint customHint = new CustomHint("Enter some text", Typeface.BOLD_ITALIC);
// CustomHint customHint = new CustomHint("Enter some text", 60f);
customEditText.setHint(customHint);
相關問題
- 1. 帶提示文本的EditText
- 2. 文本大小不同的
- 3. EditText提示文本和圖標
- 4. TextInputLayout的浮動提示文本大小
- 5. 不同的硬件文本大小
- 6. 帶有不同文本大小的TextView
- 7. 設置不同大小的文本視圖的文本
- 8. Android EditText和大量的文本
- 9. 將EditText文本轉換爲提示。 - Android
- 10. 使文本框不同大小
- 11. 選擇文本和提示等相同的文本
- 12. FF和鉻中的文本區域顯示相同的大小
- 13. 從其他的EditText小部件文本填充的EditText小部件文本
- 14. Edittext中的文本顯示不正確
- 15. Android edittext鍵入的文本不顯示
- 16. 如何在文本區域設置不同字體和大小的文本?
- 17. EditText在輸入時不顯示提示以及文本
- 18. 如何將文本和圖像設置爲edittext中的提示?
- 19. 如何顯示EditText文本?
- 20. EditText突出顯示文本
- 21. 文本不橢圓大小
- 22. 文本文件的大小
- 23. 顯示文本到文本框不同
- 24. 在不同的水平上對齊不同大小的文本
- 25. 不同字體大小和顏色的UILabel文本
- 26. 對齊不同大小和內容的文本列
- 27. IE 6和FireFox 3.6中的文本框大小不同3.6
- 28. CSS。佔位符和不同字體大小的文本框
- 29. 如何獲取文本框中顯示的文本的大小
- 30. 如何在按鈕構件上的兩行文本上設置不同的(文本大小和文本顏色)