如何在TextInputLayout中更改EditText提示的大小 [英] How to change the size of the EditText hint in TextInputLayout
问题描述
我正在尝试更改TextInputLayout
中的提示大小,但是无法正常工作.这是我想要实现的:
I'm trying to change the hint size in TextInputLayout
, but it's not working as desired. This is what I'd like to achieve:
styles.xml
<style name="TextLabel" parent="TextAppearance.Design.Hint">
<item name="android:textSize">44sp</item>
</style>
fragment.xml
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="@style/TextLabel"
android:hint="Password">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"/>
</android.support.design.widget.TextInputLayout>
当EditText
不为空时,此代码仅适用于浮动标签,但是当它为空时,我想更改EditText
本身的提示大小.
This code works only for the floating label, when the EditText
isn't empty, but I want to change the hint size in the EditText
itself, when it is empty.
推荐答案
将常规提示文本的大小设置为在充气/初始化期间添加到TextInputLayout
的EditText
文本大小.该值最终是在TextInputLayout
中的私有帮助器类上设置的,并且没有公开公开的方法或字段可以对其进行更改.
The size of the regular hint text is set to the EditText
's text size when that is added to the TextInputLayout
during inflation/initialization. This value is ultimately set on a private helper class in TextInputLayout
, and there is no publicly exposed method or field to change it.
但是,我们可以通过对TextInputLayout
进行子类化来拦截EditText
的添加,从而对文本大小进行一些调整.添加EditText
时,我们缓存其文本大小,将所需的提示大小设置为其文本大小,允许超类添加它并初始化提示,最后将EditText
的文本大小设置回其大小.原始值.
However, we can do a little juggling with the text sizes by subclassing TextInputLayout
to intercept the adding of the EditText
. When the EditText
is added, we cache its text size, set the desired hint size as its text size, allow the super class to add it and initialize the hint, and finally set the EditText
's text size back to its original value.
例如:
public class CustomTextInputLayout extends TextInputLayout {
private float mainHintTextSize;
private float editTextSize;
public CustomTextInputLayout(Context context) {
this(context, null);
}
public CustomTextInputLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.CustomTextInputLayout);
mainHintTextSize = a.getDimensionPixelSize(
R.styleable.CustomTextInputLayout_mainHintTextSize, 0);
a.recycle();
}
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
final boolean b = child instanceof EditText && mainHintTextSize > 0;
if (b) {
final EditText e = (EditText) child;
editTextSize = e.getTextSize();
e.setTextSize(TypedValue.COMPLEX_UNIT_PX, mainHintTextSize);
}
super.addView(child, index, params);
if (b) {
getEditText().setTextSize(TypedValue.COMPLEX_UNIT_PX, editTextSize);
}
}
// Units are pixels.
public float getMainHintTextSize() {
return mainHintTextSize;
}
// This optional method allows for dynamic instantiation of this class and
// its EditText, but it cannot be used after the EditText has been added.
// Units are scaled pixels.
public void setMainHintTextSize(float size) {
if (getEditText() != null) {
throw new IllegalStateException(
"Hint text size must be set before EditText is added");
}
mainHintTextSize = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, size, getResources().getDisplayMetrics());
}
}
要使用自定义的mainHintTextSize
属性,我们需要在<resources>
中添加以下内容,只需将以下文件粘贴在res/values/
文件夹中,或添加到已有的文件中即可.
To use the custom mainHintTextSize
attribute, we'll need the following in our <resources>
, which we can do by just sticking the following file in the res/values/
folder, or adding to the one that's already there.
attrs.xml
<resources>
<declare-styleable name="CustomTextInputLayout" >
<attr name="mainHintTextSize" format="dimension" />
</declare-styleable>
</resources>
如果您不想使用custom属性,则可以跳过此文件,并在上面的第三个构造函数中删除TypedArray
处理.
If you don't care to use the custom attribute, you can skip this file, and remove the TypedArray
processing in the third constructor above.
此自定义类是TextInputLayout
的直接替代,可以按原样使用.例如:
This custom class is a drop-in replacement for TextInputLayout
, and can be used just as it would. For example:
<com.mycompany.myapp.CustomTextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
app:hintTextAppearance="@style/TextLabel"
app:mainHintTextSize="12sp">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="qwerty123" />
</com.mycompany.myapp.CustomTextInputLayout>
这种方法很不错,因为它仅使用可公开访问的,记录在案的方法,但是无论在膨胀期间还是通过直接实例化,都必须在添加EditText
之前设置提示文本的大小.
This approach is nice, in that it uses only publicly-accessible, documented methods, but the hint text size must be set before the EditText
is added, whether that happens during inflation, or through direct instantiation.
这篇关于如何在TextInputLayout中更改EditText提示的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!