TextInputLayout错误右对齐

Rac*_*hit 12 layout android textinputlayout

我在TextInputLayout中包含了一个EditText.我希望在EditText下显示错误,但是对齐到屏幕的右端.

这就是我目前拥有的: 文字输入布局

错误显示如下: 现在出错了

我希望它看起来像: 我想要的是

我的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>
Run Code Online (Sandbox Code Playgroud)

我正在使用数据绑定.

我试过设置android:gravity="right"android:layout_gravity="right"上都的EditText和TextInputLayout.两者都不是我想要的方式.

我尝试在主题和风格中设置正确的重力.对错误都没有任何影响.

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

我尝试通过在此链接后使用带有AlignmentSpan的SpannableStringBuilder以编程方式将Error转移到右侧.即使这对我也不起作用.

我不知道还有什么可以尝试的.请建议.

Rac*_*hit 12

所以,由于Mike的回答,我能够找到解决方案.

我创建了一个自定义类:

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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我只需用CustomTextInputLayout替换我的TextInputLayout.奇迹般有效.非常感谢迈克.我希望我能为你的回答更多地赞美你.


Iva*_*sov 5

这是 Kotlin 中的一个不依赖于视图层次结构的 hack:

class CustomTextInputLayout @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : TextInputLayout(context, attrs, defStyleAttr) {

    override fun setError(errorText: CharSequence?) {
        super.setError(errorText)
        with(findViewById<TextView>(R.id.textinput_error)) {
            // this will work as long as errorView's layout width is
            // MATCH_PARENT -- it is, at least now in material:1.2.0-alpha06
            textAlignment = TextView.TEXT_ALIGNMENT_VIEW_END
        }
    }
}
Run Code Online (Sandbox Code Playgroud)