2017-03-03 16 views
6

TextInputLayout 내에 포함 된 EditText가 있습니다. EditText 아래에 오류를 표시하고 싶지만 화면의 오른쪽 끝으로 맞 춥니 다. 오류는 다음과 같이 표시됩니다 Text Input LayoutTextInputLayout 오른쪽 맞춤 오류

: 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> 

저는 데이터 바인딩을 사용합니다.

EditText와 TextInputLayout 둘 다에서 android:gravity="right"android:layout_gravity="right"을 설정해 보았습니다. 둘 다 내가 원하는 방식으로 작동하지 않습니다.

나는 테마와 스타일에 올바른 중력을 설정해 보았습니다. 어느 쪽도 오류에 어떤 영향도 미치지 않습니다.

app:errorTextAppearance="@style/right_error"에 적용되는 스타일에 올바른 중력을 시도했습니다. 심지어 이것은 작동하지 않습니다.

this link 다음에 나오는 AlignmentSpan과 함께 SpannableStringBuilder를 사용하여 프로그래밍 방식으로 오류를 오른쪽으로 시프트했습니다. 그것조차 저를 위해 작동하지 않는다.

나는 무엇을 시도해야할지 모르겠습니다. 제발 제안 해주세요.

+2

[my answer here] (http://stackoverflow.com/a/40706872)의 옵션 중 하나를 사용하여 'TextView' 오류를 확인하고'setGravity (Gravity.RIGHT) '를 호출 할 수 있습니다 . 나는 그것을 테스트하지는 않았지만 IIRC, 오류 'TextView'는'match_parent' 너비가 있으므로 설명하는대로 텍스트를 정렬해야합니다. –

답변

6

덕분에 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. 나는이 대답에 대해 당신에게 더 많이 신용 할 수 있었으면 좋겠다.

+0

안녕하세요, edittext 및 오른쪽 정렬 된 모서리에 오류 메시지를 표시하려고합니다. 그 생각은 어떨까요? – ViVekH

+0

@ViVekH, 원하는 것을 조금 더 설명해 주시겠습니까? – Rachit