2016-09-16 13 views
6

enter image description here Tôi đã tạo EditText bên trong TextInputLayout. Tôi đang thiết lập drawableLeft thành EditText khi chạy trong mã của tôi, nhưng ngay sau khi tôi thêm drawableLeft gợi ý nổi bên trong TextInputLayout dịch chuyển sang phải để lại khoảng trống bằng chiều rộng có thể vẽ được. Nhưng tôi không muốn không gian đó trong gợi ý, vì vậy hãy giúp tôi giải quyết vấn đề này !!Thêm một drawableLeft vào EditText chuyển gợi ý về phía bên phải, nếu edittext nằm trong TextInputlayout

+0

xin vui lòng gửi xml của bạn –

+0

Sử dụng _setCompoundDrawablePadding_ – Piyush

Trả lời

18

TextInputLayout sử dụng lớp trợ giúp - CollapsingTextHelper - để thao tác văn bản gợi ý của nó. Cá thể của trình trợ giúp này là riêng tư và không có thuộc tính nào liên kết với bố cục của nó được hiển thị, vì vậy chúng tôi sẽ cần sử dụng một chút phản ánh để truy cập vào nó. Hơn nữa, các thuộc tính của nó được thiết lập và tính toán lại mỗi khi TextInputLayout được đặt ra, do đó, nó có ý nghĩa đối với lớp con TextInputLayout, ghi đè phương thức onLayout() của nó và thực hiện điều chỉnh của chúng tôi ở đó.

import android.content.Context; 
import android.graphics.Rect; 
import android.support.design.widget.TextInputLayout; 
import android.util.AttributeSet; 
import java.lang.reflect.Field; 
import java.lang.reflect.InvocationTargetException; 
import java.lang.reflect.Method; 

public class CustomTextInputLayout extends TextInputLayout { 
    private Object collapsingTextHelper; 
    private Rect bounds; 
    private Method recalculateMethod; 

    public CustomTextInputLayout(Context context) { 
     this(context, null); 
    } 

    public CustomTextInputLayout(Context context, AttributeSet attrs) { 
     this(context, attrs, 0); 
    } 

    public CustomTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 

     init(); 
    } 

    @Override 
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 
     super.onLayout(changed, left, top, right, bottom); 

     adjustBounds(); 
    } 

    private void init() { 
     try { 
      Field cthField = TextInputLayout.class.getDeclaredField("mCollapsingTextHelper"); 
      cthField.setAccessible(true); 
      collapsingTextHelper = cthField.get(this); 

      Field boundsField = collapsingTextHelper.getClass().getDeclaredField("mCollapsedBounds"); 
      boundsField.setAccessible(true); 
      bounds = (Rect) boundsField.get(collapsingTextHelper); 

      recalculateMethod = collapsingTextHelper.getClass().getDeclaredMethod("recalculate"); 
     } 
     catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException e) { 
      collapsingTextHelper = null; 
      bounds = null; 
      recalculateMethod = null; 
      e.printStackTrace(); 
     } 
    } 

    private void adjustBounds() { 
     if (collapsingTextHelper == null) { 
      return; 
     } 

     try { 
      bounds.left = getEditText().getLeft() + getEditText().getPaddingLeft(); 
      recalculateMethod.invoke(collapsingTextHelper); 
     } 
     catch (InvocationTargetException | IllegalAccessException | IllegalArgumentException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

Lớp tùy chỉnh này là thay thế thả cho số TextInputLayout thông thường và bạn sẽ sử dụng nó theo cùng một cách. Ví dụ:

<com.mycompany.myapp.CustomTextInputLayout 
    android:id="@+id/text_input_layout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="Model (Example i10, Swift, etc.)" 
    app:hintTextAppearance="@style/TextLabel"> 

    <android.support.design.widget.TextInputEditText 
     android:id="@+id/edit_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:drawableLeft="@drawable/bmw" 
     android:text="M Series" /> 

</com.mycompany.myapp.CustomTextInputLayout> 

screenshot

+0

nó không làm việc bây giờ trả lại một lỗi ** CollapsingTextHelper không được công khai trong android.support.design.widget; không thể truy cập từ gói bên ngoài ** –

+0

Chỉ cần xóa 'CollapsingTextHelper'' import' ở đầu lớp. Nó không thực sự được sử dụng ở bất cứ đâu trong ví dụ. Tôi chỉ quên để làm sạch các hàng nhập khẩu trước khi tôi đăng, rõ ràng. –

+1

Cảm ơn bạn đã làm việc. –

-1

Có, Đây là vấn đề thường gặp mà tôi gặp phải gần đây. Tôi đã giải quyết vấn đề với mã một dòng đơn giản: Đặt khoảng cách giữa gợi ý và drawbleleft bằng cách sử dụng drawble padding. Nếu bạn đang thêm drawble vào thời gian chạy, chỉ cần thêm drawblepadding vào xml trước bàn tay hoặc bạn có thể thêm động vào ô xếp chồng.

-1
editText.setCompoundDrawablePadding(your padding value); 

Hãy thử và cho tôi biết. Nó làm việc cho tôi.

Các vấn đề liên quan