android TextInputLayout错误文本右对齐不工作

bnl4lu3b  于 2023-06-04  发布在  Android
关注(0)|答案(1)|浏览(187)

按照设计,我需要在右侧的TextInputLayout错误文本,探索和尝试下面的风格,但没有成功。

<style name="Theme.Input" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
        <item name="hintEnabled">false</item>
        <item name="boxCornerRadiusBottomEnd">@dimen/input_box_corner_radius</item>
        <item name="boxCornerRadiusBottomStart">@dimen/input_box_corner_radius</item>
        <item name="boxCornerRadiusTopEnd">@dimen/input_box_corner_radius</item>
        <item name="boxCornerRadiusTopStart">@dimen/input_box_corner_radius</item>
        <item name="errorEnabled">true</item>
        <item name="errorTextColor">@color/red</item>
        <item name="boxStrokeErrorColor">@color/red</item>
        <item name="android:textColor">@color/colorInputText</item>
        <item name="iconEndPadding">@dimen/std_16dp</item>
        <item name="errorTextAppearance">@style/Theme.EZazi.Input.Error</item>
        <item name="android:textSize">@dimen/input_inner_size</item>
        <item name="boxStrokeWidth">@dimen/input_stroke_size</item>
        <item name="boxStrokeWidthFocused">@dimen/input_stroke_size</item>
    </style>

    <style name="Theme.Input.Error" parent="TextAppearance.Design.Error">
        <item name="android:textColor">@color/red</item>
        <item name="android:textSize">@dimen/caption</item>
        <item name="android:textAlignment">viewEnd</item>
    </style>

我想达到的设计目的

应用主题后,它仍然在左侧。

还尝试了自定义方法。

public class RightAlignErrorTextInputLayout extends TextInputLayout {
    public RightAlignErrorTextInputLayout(@NonNull Context context) {
        super(context);
        init();
    }

    public RightAlignErrorTextInputLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RightAlignErrorTextInputLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
    }

    @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.END);
                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();
        }
    }
}

有谁可以帮助实现错误文本在右侧与TextInputLayout
您的帮助将不胜感激,先谢了。

7bsow1i6

7bsow1i61#

创建一个自定义的TextInputLayout,并简单地将TextInputLayout替换为CustomTextInputLayout

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) {
            e.printStackTrace();
        }
    }
}

相关问题