【问题标题】:How to change hint text size in TextInputLayout如何在 TextInputLayout 中更改提示文本大小
【发布时间】:2016-02-04 14:29:01
【问题描述】:

假设我想更改文本大小。我是在代码中做的,它看起来像这样:

_textInputLayout.EditText.SetTextSize(Android.Util.ComplexUnitType.Dip, 40);

当我在条目中写入文本时,它看起来像 40dip 文本。但是当条目为空时,提示文本看起来像 16-18dip。

有没有办法改变提示文字的大小?

【问题讨论】:

  • 我没有尝试使用TextInputLayout,但只是普通的EditText 和对SetTextSize 的调用正在调整Hint 大小的文本以及输入时的实际文本进入控件并保持它们相同的大小。如果你能制作一个小演示,我会进一步看看你有什么?如果是这样,我的详细信息在我的个人资料中。

标签: android xamarin xamarin.forms android-textinputlayout textinputlayout


【解决方案1】:

可以通过样式更改最终提示大小/浮动标签大小,并使用以下内容调用SetHintTextAppearance:-

_nativeView.SetHintTextAppearance(App6.Droid.Resource.Style.MyTextInputLayout);

MyTextInputLayout 是这样的:-

<style name="MyTextInputLayout" parent="@android:style/TextAppearance">
    <item name="android:textColor">@color/blue</item>
    <item name="android:textSize">44sp</item>
</style>

但是,这种风格的textSize 仅适用于最终目的地,当您开始输入一些文本时。

据我所见,包括对象的属性,目前似乎无法更改提示的起始字体大小?

EditText 暴露在哪里,您可以在那里更改内容。 Hint 部分根本不由它处理,而是由TextInputLayout 处理。似乎没有任何对象可以访问以专门为 Hint 自定义此设置。

【讨论】:

    【解决方案2】:

    您可以通过在字符串资源中设置大小来做到这一点。

    例如:

    <string name="edittext_hint"><font size="15">Hint here!</font></string>
    

    然后在你的 XML 中写

    android:hint="@string/edittext_hint"
    

    这将导致提示文本更小,但输入文本的原始大小。

    或者像这样:

    MYEditText.addTextChangedListener(new TextWatcher(){
    
                @Override
                public void afterTextChanged(Editable arg0) {
                    // TODO Auto-generated method stub
    
                }
    
                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1,
                        int arg2, int arg3) {
                    // TODO Auto-generated method stub
    
                }
    
                @Override
                public void onTextChanged(CharSequence arg0, int start, int before,
                        int count) {
                    if (arg0.length() == 0) { 
                        // No entered text so will show hint
                        editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, mHintTextSize);
                    } else {
                        editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, mRealTextSize);
                    }
                }
        });
    

    【讨论】:

    猜你喜欢
    • 2015-12-26
    • 1970-01-01
    • 1970-01-01
    • 2015-09-15
    • 1970-01-01
    • 2017-03-06
    • 2020-06-26
    • 1970-01-01
    相关资源
    最近更新 更多