如何在不更改EditText中的文本大小的情况下更改提示文本大小

时间:2016-08-23 17:06:51

标签: android android-layout android-fragments android-edittext

我有一个EditText输入字段。我在其中添加了一个提示。现在我想改变提示文本的大小,但是当我这样做时,它也会影响文本的大小。请指导我如何分别更改提示和文本的大小,并为提示和文本提供不同的字体。

<EditText
    android:layout_width="0dp"
    android:layout_height="50dp"
    android:layout_weight="1"
    android:textSize="12sp"
    android:textColor="#ffffff"                            
    android:fontFamily="sans-serif-light"
    android:hint="MM/YY"
    android:textColorHint="@color/white" />  

4 个答案:

答案 0 :(得分:27)

您可以在资源文件中设置它。

例如:

<string name="hint"><font size="20">Hint!</font></string>

你的XML:

android:hint="@string/hint"

答案 1 :(得分:8)

提示和文本是独占的,如果其中一个可见,则另一个不可用。

因此,您可以更改lapply(names(SPLIT.DATA), function(nm) write.csv(SPLIT.DATA[[nm]], paste0(nm, ".csv"), row.names = FALSE, quote = FALSE)) 的属性,具体取决于它是否为空(提示是否可见)或不是(文本可见)。

例如:

EditText

答案 2 :(得分:1)

您可以将文本大小设置为较小的所需值,然后设置文本侦听器以在输入某些文本后更改文本大小。

答案 3 :(得分:1)

使用HTML是可以的,但它不灵活。例如,您无法设置确切的大小。我将提供一个替代解决方案,您可以设置:

  • 新提示字体
  • 新提示大小
  • 新提示风格

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);