【问题标题】:How can I change the position of drawable in TextInputEditText?如何更改 TextInputEditText 中可绘制的位置?
【发布时间】:2020-08-20 22:45:23
【问题描述】:
【问题讨论】:
标签:
android-textinputlayout
android-textinputedittext
【解决方案1】:
在互联网上不断搜索后,我找到了解决方案。首先在这个post,它在接受的答案中建议,不可能将图标放在顶部,但是,您必须将 EditText 包装在 LinearLayout 中,然后他将
下面的示例代码:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@android:drawable/edit_text"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@android:drawable/ic_menu_send"
android:scaleType="fitStart"
android:contentDescription="message icon"
/>
<EditText
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@null"
android:padding="4dp"
android:text="Message"
android:inputType="textMultiLine"
android:gravity="top"
android:layout_weight="1" />
我接受了这个想法,并通过将 TextInputLayout 内的图标与包含在 LinearLayout 中的 FrameLayout 重叠来实现它。我的代码如下所示:
<LinearLayout
android:layout_marginTop="28dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<FrameLayout
android:background="@drawable/edt_bg_selector_fafa_orange"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilComentReporte"
android:gravity="top|start"
app:errorTextColor="@color/colorRojo"
app:errorEnabled="true"
android:maxLines="10"
android:background="@drawable/edt_bg_selector_fafa_orange"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/etComentReporte"
android:paddingBottom="10dp"
android:background="@null"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:lines="5"
android:gravity="top|start"
android:singleLine="false"
android:inputType="textMultiLine|textAutoComplete"
android:drawablePadding="15dp"
android:fontFamily="@font/lato"
android:hint="@string/sign_describe_tu_problema"
android:paddingStart="20dp"
android:textSize="16sp" />
</com.google.android.material.textfield.TextInputLayout>
<ImageView
android:paddingTop="10dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scaleType="fitStart"
android:src="@drawable/ic_contacto_nar" />
</FrameLayout>
</LinearLayout>
它在设备上看起来像这样:
The view now