gpt4 book ai didi

android - TextInputLayout 错误右对齐

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:39:57 25 4
gpt4 key购买 nike

我有一个包含在 TextInputLayout 中的 EditText。我希望在 EditText 下显示错误,但与屏幕右端对齐。

这是我目前拥有的: Text Input Layout

错误显示如下: Error right now

我希望它看起来像什么: What I want

我的 XML 是这样的:

        <android.support.design.widget.TextInputLayout
android:id="@+id/text_input_email"
style="@style/forgot_pass_text_inputlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv_enter_email_message"
android:layout_marginTop="@dimen/email_padding_top"
android:hintTextAppearance="@{forgotPasswordVM.hintTextAppearance}"
android:theme="@style/forgot_pass_til_state"
app:error="@{forgotPasswordVM.email.textInputError}">

<EditText
android:id="@+id/actv_email"
style="@style/forgot_pass_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/email_address"
android:imeActionId="@+id/btn_submit"
android:imeOptions="actionSend"
android:inputType="textEmailAddress"
android:maxLines="1"
android:onEditorAction="@{forgotPasswordVM.onEditorAction}"
android:singleLine="true"
app:binding="@{forgotPasswordVM.email.text}" />
</android.support.design.widget.TextInputLayout>

我正在使用数据绑定(bind)。

我尝试在 EditText 和 TextInputLayout 上设置 android:gravity="right"android:layout_gravity="right"。两者都不像我想要的那样工作。

我已经尝试在主题和样式中设置正确的引力。两者都对错误没有任何影响。

我已经在 app:errorTextAppearance="@style/right_error" 中应用的样式上尝试了正确的引力。即使这样也行不通。

我尝试通过在 this link 之后使用带有 AlignmentSpan 的 SpannableStringBuilder 以编程方式将错误向右移动.即使那样对我也不起作用。

我不确定还能尝试什么。请提出建议。

最佳答案

感谢Mike's answer我能够找出解决方案。

我创建了一个自定义类:

public class CustomTextInputLayout extends TextInputLayout {

public CustomTextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
public void setErrorEnabled(boolean enabled) {
super.setErrorEnabled(enabled);

if (!enabled) {
return;
}

try {
Field errorViewField = TextInputLayout.class.getDeclaredField("mErrorView");
errorViewField.setAccessible(true);
TextView errorView = (TextView) errorViewField.get(this);
if (errorView != null) {
errorView.setGravity(Gravity.RIGHT);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.END;
errorView.setLayoutParams(params);
}
}
catch (Exception e) {
// At least log what went wrong
e.printStackTrace();
}
}
}

现在我只是用 CustomTextInputLayout 替换了我的 TextInputLayout。奇迹般有效。非常感谢Mike .我希望我能把这个答案更多地归功于你。

关于android - TextInputLayout 错误右对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42575825/

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