gpt4 book ai didi

android - 以编程方式在 TextInputLayout 中设置 EditText 的提示大小

转载 作者:行者123 更新时间:2023-11-29 01:16:20 25 4
gpt4 key购买 nike

我正在使用包裹在 TextInputLayout 中的 EditText。以编程方式设置 EditTexttextSize 适用于输入文本本身。但是,它不适用于 EditText 的提示文本,提示文本在输入为空时显示。

我是不是遗漏了什么或者这是一个错误?如果是后者,是否有解决方法?

为了说明,这里是我的代码 fragment :

TextInputLayout 在自定义 View 中:

<android.support.design.widget.TextInputLayout
android:id="@+id/password_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/default_password_hint"
android:textColorHint="@color/background_gray">
<EditText
android:id="@+id/txt_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"/>
</android.support.design.widget.TextInputLayout>

因为我想重用具有不同文本大小的View,我通过自定义属性textSize传递文本大小:

<declare-styleable name="PasswordInputField">
<attr name="textSize" format="dimension"/>
<attr name="hint" format="string"/>
</declare-styleable>

并按以下方式在我的自定义 View 中应用它:

if ( attrs != null )
{
TypedArray ta = context.obtainStyledAttributes( attrs, R.styleable.PasswordInputField, 0, 0 );
try
{
String hint = ta.getString( R.styleable.PasswordInputField_hint );
if ( hint != null )
{
passwordInputLayout.setHint( hint );
}
float textSize = ta.getDimensionPixelSize( R.styleable.PasswordInputField_textSize, 0 );
if ( textSize > 0 )
{
passwordEditText.setTextSize( TypedValue.COMPLEX_UNIT_PX, textSize );
}
}
finally
{
ta.recycle();
}
}

最佳答案

样式.xml

<style name="TextAppearence.App.TextInputLayout" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/white</item>
<item name="android:textColorHint">@color/white</item>
<item name="android:textSize">18sp</item>
</style>

在 TextInput 布局中

                <TextInputLayout
android:id="@+id/til_signup_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/name"
app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout">

<EditText
android:id="@+id/ed_signup_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@color/white" />
</TextInputLayout>

关于android - 以编程方式在 TextInputLayout 中设置 EditText 的提示大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39346213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com