问题
I've an EditText enclosed within a TextInputLayout. I wish to display errors under the EditText, but aligned to the right end of the screen.
This is what I currently have:
The error is displayed like this:
What I want it to look like:
My XML is this:
<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>
I'm using data-binding.
I've tried setting android:gravity="right"
and android:layout_gravity="right"
on both the EditText and the TextInputLayout. Neither works the way I want it to.
I've tried setting right gravity in the theme as well as the style. Neither has any effect on the error.
I've tried right gravity on the style that is applied within app:errorTextAppearance="@style/right_error"
. Even this doesn't work.
I tried programmatically shifting the Error to the right by using a SpannableStringBuilder with an AlignmentSpan following this link. Even that doesn't work for me.
I'm not sure what else to try. Please suggest.
回答1:
So thanks to Mike's answer I was able to figure out the solution.
I created a custom class:
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();
}
}
}
Now I simply replaced my TextInputLayout with the CustomTextInputLayout. Works like a charm. Thanks a lot Mike. I wish I could credit you more for this answer.
回答2:
My custom class is in Kotlin
class CustomTextInputLayout(context: Context, attrs: AttributeSet) :
TextInputLayout(context, attrs) {
override fun setErrorEnabled(enabled: Boolean) {
super.setErrorEnabled(enabled)
if (!enabled) {
return
}
try {
val layout = this;
val errorView : TextView = ((this.getChildAt(1) as ViewGroup).getChildAt(0) as ViewGroup).getChildAt(0) as TextView
(layout.getChildAt(1) as ViewGroup).layoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT
(layout.getChildAt(1) as ViewGroup).getChildAt(0).layoutParams.width = FrameLayout.LayoutParams.MATCH_PARENT
errorView.gravity = Gravity.END
} catch (e: Exception) {
e.printStackTrace()
}
}
}
回答3:
If selected answer not works. Another solution is below:
Create custom class as below:
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
{
TextView errorView = this.findViewById(R.id.textinput_error);
FrameLayout errorViewParent = (FrameLayout) errorView.getParent();
errorViewParent.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
errorView.setGravity(Gravity.CENTER); // replace CENTER with END or RIGHT
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Then create your custom TextInputLayout as:
<CustomTextInputLayout
android:id="@+id/til_first_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/activity_horizontal_margin"
app:layout_constraintBottom_toTopOf="@+id/til_last_name">
<EditText
android:id="@+id/et_first_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/first_name"
android:gravity="right"
/>
</CustomTextInputLayout>
After that initialize in your code as:
private CustomTextInputLayout tilFirstName;
tilFirstName = view.findViewById(R.id.til_first_name);
Congratulations! You have done what you have required.
来源:https://stackoverflow.com/questions/42575825/textinputlayout-error-right-align