XML格式的EditText的提示大小

时间:2015-08-01 11:00:56

标签: android html css xml android-edittext

我希望hint的{​​{1}}大小小于真实文本。

目前我知道两个解决方案:

  1. 以编程方式使用EditText

  2. 在XML中使用Html.fromHtml()

  3. 但是,我不想使用第一个解决方案,因为我希望将提示直接写在我的布局XML中,而对于第二个解决方案,我不喜欢使用<font size="">元素,因为(如果我没错的话)我无法明确font措施。此外,dp元素在HTML5中已弃用,因此它属于“旧式”。

    我尝试在我的XML中使用带有font属性(来自CSS)的span元素,但它不起作用。还有其他最新的解决方案吗?

1 个答案:

答案 0 :(得分:1)

我不知道你可以通过xml来实现这一点(除了在提示文本中粘贴html标签)。

但是还有另一种方法没有在上面概述,这将允许您将字体大小保持在sp并与java代码分开:

final int hintSize = <read_this_from_xml_resources_but_take_into_account_density>;
final int textSize = <read_this_from_xml_resources_but_take_into_account_density>;
editText.addTextChangedListener(new TextWatcher(){
    @Override
    public void afterTextChanged(Editable arg0) {
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
    }

    @Override
    public void onTextChanged(CharSequence arg0, int start, int before, int count) {
        editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, arg0.length() > 0 ? textSize : hintSize);
    }
});