2016-10-19 97 views
1

有没有办法创建一个看起来像这个图像上的输入字段 - 左侧的图标,右侧的输入 - 只使用默认的TextInputLayout?材料设计输入文本

我试过几种组合,图标总是留在TextInputEditText中。我尝试将TextInputLayout方向更改为水平方向,但它不起作用。如果没有将ImageView和EditText放置在另一个ViewGroup中,是否有这样做?

下面是我用来创建输入字段的代码,但图标仍保留在TextInputEditText中。

<android.support.design.widget.TextInputLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="Test" 
     > 
     <android.support.design.widget.TextInputEditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:drawableStart="@drawable/ic_bank_card_back_grey_24dp" 
      android:drawablePadding="@dimen/default_padding"    
      /> 
    </android.support.design.widget.TextInputLayout> 

Material Design Input Text

+1

我会假设这是一个'TableLayout',有三列的图标,现场和'Spinner',但使用'uiautomatorviewer'检查的结构该用户界面并找出答案。 – CommonsWare

+0

可能会帮助http://stackoverflow.com/questions/3554377/handling-click-events-on-a-drawable-within-an-edittext – zombie

回答

1

这是否符合要求? :

<android.support.design.widget.TextInputLayout 
    android:id="@+id/TextInputLayout1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <android.support.v7.widget.AppCompatEditText 
     android:id="@+id/EditText1" 
     android:hint="code" 
     android:text="0044" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:imeOptions="actionNext" 
     android:inputType="phone" 
     android:minWidth="350dp" 
     android:drawableLeft="@drawable/cross" 
     android:drawableStart="@drawable/cross" 
     android:textColor="#00ff00" 
     android:textColorHint="#ff0000" 
     android:textCursorDrawable="@null" 
     /> 
</android.support.design.widget.TextInputLayout> 

从码图像: image from code

+0

Nop。这跟我一样。如果您查看附加图像,可以看到图标上没有下划线。我可以使用LinearLayout和ImageView实现类似的功能,但我认为这样做有更好的方法。任何其他想法? –

+0

@JeffersonTavares这就是'EditText'中的复合drawables的工作方式。要改变这种行为,你必须继承你正在使用的'EditText'类,或者做一些相当笨拙的反射,或者在背景drawable上使用一些填充黑客,等等。使用单独的'ImageView' ,这是你发布的图片似乎在做什么。 –

+0

谢谢你。我要关闭这个问题,因为这对其他用户不会有太大的帮助。由于TextInputLayout继承自LinearLayout,因此它应该是一个简单而容易的任务,它将ImageView和EditText包装在它内部,但有时候它并不像预期的那样工作,我猜是xD –