2017-03-03 65 views
6

我在TextInputLayout中包含了一個EditText。我希望在EditText下顯示錯誤,但是與屏幕的右端對齊。TextInputLayout錯誤對齊

這是我目前有: 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> 

我是使用數據綁定。

我已經試過上都的EditText和TextInputLayout設置android:gravity="right"android:layout_gravity="right"。它們都不以我想要的方式工作。

我已經嘗試在主題中設置權重以及樣式。對錯誤也沒有任何影響。

我已經嘗試了在app:errorTextAppearance="@style/right_error"內應用的風格的權重。即使這不起作用。

我試圖編程方式通過使用一SpannableStringBuilder具有以下this link一個AlignmentSpan移位錯誤到右側。即使這對我不起作用。

我不知道還有什麼可以嘗試的。請建議。

+2

您可以嘗試使用[我的答案](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(); 
     } 
    } 
} 

現在我簡單地更換我的TextInputLayout與CustomTextInputLayout。 工程就像一個魅力。非常感謝Mike。我希望我能更多地讚揚你這個答案。

+0

嗨,即時通訊試圖顯示錯誤消息的編輯文本和右對齊的角落,任何想法我怎麼做? – ViVekH

+0

@ViVekH,你能解釋一下你想要什麼嗎? – Rachit