2016-03-05 31 views
0

我有一个自定义EditText字符与一些简单的字符和图像。如何填充自定义EditText字符的底部?

看这样的形象:enter image description here

但我想的图象填补这样的形象底:

enter image description here

这是我pwd_bullet.png:enter image description here

这是我的代码:

  1. custom_edittext.xml

    <?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#5b5555" 
    tools:context="com.example.user.myapplication.Custom_Edittext"> 
        <LinearLayout 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:background="#fff"> 
         <EditText 
          android:layout_width="match_parent" 
          android:layout_height="60dp" 
          android:id="@+id/editText" 
          android:inputType="text" 
          android:gravity="center_vertical" 
          android:background="@null"/> 
        </LinearLayout> 
    </RelativeLayout> 
    
  2. Custom_Edittext.java

    public class Custom_Edittext extends Activity implements TextWatcher{ 
        EditText editText; 
        Spannable.Factory spannableFactory; 
        int lastIndex = -1; 
    
    protected void onCreate(Bundle savedInstanceState) { 
        enter code here`super.onCreate(savedInstanceState); 
        setContentView(R.layout.custom_edittext); 
        editText = (EditText) findViewById(R.id.editText); 
        spannableFactory = Spannable.Factory 
         .getInstance(); 
        editText.addTextChangedListener(this); 
    } 
    public Spannable getIconText(Context context, CharSequence text, int index) { 
        Spannable spannable = spannableFactory.newSpannable(text); 
        if (index>lastIndex) { 
         spannable.setSpan(new ImageSpan(context, R.drawable.pwd_bullet), 
          index, index + 1, 
          Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
        } 
        lastIndex=index; 
        return spannable; 
    } 
    
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {} 
    
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
        if (start>4) { 
         editText.removeTextChangedListener(this); 
         editText.setText(getIconText(getApplicationContext(), s, start)); 
         editText.addTextChangedListener(this); 
         editText.setSelection(s.length()); 
        } 
    } 
    
    public void afterTextChanged(Editable s) {} 
    } 
    

回答

1

在绘制

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
<item 
    android:id="@+id/layer_list" 
    android:drawable="@drawable/pwd_bullet" 
    android:width="10dp" 
    android:height="10dp" 
    android:left="3dp" 
    android:bottom="5dp"/> 
</layer-list> 
1

没有必要使用填充在这种情况下,只需使用一个线性布局,因为你的项目是在一条直线上,并确保将“线性”布局设置为方向,重力如下所示。这对我有效。祝你好运

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical" 
    android:orientation="horizontal"> 
    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="60dp" 
     android:id="@+id/editText" 
     android:inputType="text" 
     android:gravity="center_vertical" 
     android:background="@null" 
     android:text="Simple Text" /> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/pwd_bullet.png"/> 
</LinearLayout> 
+1

创建一个XML定制pwd_bullet这是一个EditText没有的EditText和ImageView的 –