使用drawableLeft的图像不适合EditText字段

时间:2023-01-14 11:45:43

I'm trying to make an image fit into the EditText itself using drawableLeft, without any padding or "whitespaces" around it.

我正在尝试使用drawableLeft使图像适合EditText本身,周围没有任何填充或“空格”。

This is what the image looks like for now

这就是图像现在的样子

使用drawableLeft的图像不适合EditText字段

This is how it should look like

它应该是这样的

使用drawableLeft的图像不适合EditText字段

I tried increasing the size of the image itself but it made the EditText expand even more.

我尝试增加图像本身的大小,但它使EditText扩展得更多。

Here's the XML file:

这是XML文件:

 <EditText
    android:layout_marginTop="6dp"
    android:id="@+id/login_password"
    android:layout_width="315dp"
    android:layout_height="wrap_content"
    android:textSize="17dp"
    android:textColor="#8d8e8f"
    android:hint="Password"

    android:drawableLeft="@drawable/sample"
    android:singleLine="true"
    android:inputType="textPassword"
    android:layout_below="@+id/login_name"
    android:layout_centerHorizontal="true" />

How can the image fit inside the EditText? Does it something to do about the image itself?

图像如何适应EditText?对图像本身有什么看法吗?

3 个解决方案

#1


1  

You can try the LinearLayout for it. Means in the LinearLayout , you should put the Editext's drawable image as the background , and than divide it in to three parts,1st part is for the password_image , 2nd one for your EditText and and 3rd last one for is your INVISIBLE View, which prevent , your EditText value to touch at end

您可以尝试使用LinearLayout。在LinearLayout中的意思,你应该把Editext的drawable图像作为背景,然后把它分成三部分,第一部分用于password_image,第二部分用于EditText,第三部分用于你的INVISIBLE View,它可以防止,你的EditText值要在最后触摸

                   <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:background="@drawable/EDITEXT_DRAWABLE"
                        android:gravity="center_vertical"
                        android:padding="5dp"
                        android:weightSum="2">

                        <ImageView

                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.4"
                            android:gravity="left|center"
                            android:src="@drawable/sample" />


                        <EditText
                            android:id="@+id/password_et"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_weight="1.5"
                            android:background="@android:color/transparent"
                            android:ems="10"
                            android:gravity="center"
                            android:hint="********"
                            android:inputType="textPassword"
                            android:paddingLeft="-10dp"
                            android:singleLine="true"
                            android:imeOptions="actionDone"
                            android:textColor="@android:color/white"
                            android:textColorHint="@android:color/white" />

                        <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical|left"
                            android:layout_weight="0.1"
                            android:paddingLeft="8dp"
                            android:paddingRight="8dp"
                            android:src="@drawable/sample"
                            android:visibility="invisible" /> ///PUT THE VIEW INVIIBLE

                    </LinearLayout>

#2


0  

Just use a Fram layout it works well use a frame layout and inside it edit text and an image view is created set the background of image view to transperent and gravity to the right or to the left

只需使用Fram布局,它可以很好地使用框架布局,并在其中编辑文本并创建图像视图,将图像视图的背景设置为向右或向左移动和重力

<FrameLayout
android:layout_width="match_parent"
android:layout_height="25dp">

<EditText
    android:layout_width="match_parent"
    android:layout_height="25dp"
    android:layout_gravity="left|center_vertical"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:background="@drawable/border_round"
    android:drawableLeft="@color/place_autocomplete_search_text"
    android:drawableStart="@drawable/places_ic_search"
    android:drawableRight="@drawable/arrowxml">
</EditText>
<ImageView
    android:layout_width="25dp"
    android:background="@android:color/transparent"
    android:scaleType="centerCrop"
    android:layout_gravity="right"
    android:src="@drawable/searcharrow"
    android:layout_height="25dp" />

#3


-1  

use this...........

             <EditText
                android:id="@+id/user_name"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/editback"
                android:drawableLeft="@drawable/useremail"
                android:drawablePadding="10dp"
                android:hint="@string/user_name"
                android:padding="7dp"
                android:singleLine="true"
                android:textColor="#9291A1"
                android:textSize="15sp" />

#1


1  

You can try the LinearLayout for it. Means in the LinearLayout , you should put the Editext's drawable image as the background , and than divide it in to three parts,1st part is for the password_image , 2nd one for your EditText and and 3rd last one for is your INVISIBLE View, which prevent , your EditText value to touch at end

您可以尝试使用LinearLayout。在LinearLayout中的意思,你应该把Editext的drawable图像作为背景,然后把它分成三部分,第一部分用于password_image,第二部分用于EditText,第三部分用于你的INVISIBLE View,它可以防止,你的EditText值要在最后触摸

                   <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:background="@drawable/EDITEXT_DRAWABLE"
                        android:gravity="center_vertical"
                        android:padding="5dp"
                        android:weightSum="2">

                        <ImageView

                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.4"
                            android:gravity="left|center"
                            android:src="@drawable/sample" />


                        <EditText
                            android:id="@+id/password_et"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_weight="1.5"
                            android:background="@android:color/transparent"
                            android:ems="10"
                            android:gravity="center"
                            android:hint="********"
                            android:inputType="textPassword"
                            android:paddingLeft="-10dp"
                            android:singleLine="true"
                            android:imeOptions="actionDone"
                            android:textColor="@android:color/white"
                            android:textColorHint="@android:color/white" />

                        <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical|left"
                            android:layout_weight="0.1"
                            android:paddingLeft="8dp"
                            android:paddingRight="8dp"
                            android:src="@drawable/sample"
                            android:visibility="invisible" /> ///PUT THE VIEW INVIIBLE

                    </LinearLayout>

#2


0  

Just use a Fram layout it works well use a frame layout and inside it edit text and an image view is created set the background of image view to transperent and gravity to the right or to the left

只需使用Fram布局,它可以很好地使用框架布局,并在其中编辑文本并创建图像视图,将图像视图的背景设置为向右或向左移动和重力

<FrameLayout
android:layout_width="match_parent"
android:layout_height="25dp">

<EditText
    android:layout_width="match_parent"
    android:layout_height="25dp"
    android:layout_gravity="left|center_vertical"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:background="@drawable/border_round"
    android:drawableLeft="@color/place_autocomplete_search_text"
    android:drawableStart="@drawable/places_ic_search"
    android:drawableRight="@drawable/arrowxml">
</EditText>
<ImageView
    android:layout_width="25dp"
    android:background="@android:color/transparent"
    android:scaleType="centerCrop"
    android:layout_gravity="right"
    android:src="@drawable/searcharrow"
    android:layout_height="25dp" />

#3


-1  

use this...........

             <EditText
                android:id="@+id/user_name"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/editback"
                android:drawableLeft="@drawable/useremail"
                android:drawablePadding="10dp"
                android:hint="@string/user_name"
                android:padding="7dp"
                android:singleLine="true"
                android:textColor="#9291A1"
                android:textSize="15sp" />