Place Icon at Top Left Corner in Multiline EditTex

2019-06-01 05:50发布

问题:

How Do I place icon at top left corner in multiline EditText, here is what I am using :

<EditText
        android:id="@+id/editComments"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:paddingBottom="20dp"
        android:paddingRight="10dp"
        android:layout_margin="10dp"
        android:hint="Comments"
        android:inputType="textMultiLine"
        android:lines="4"
        android:drawablePadding="20dp"
        android:minLines="4"
        android:gravity="top|left"
        android:drawableLeft="@drawable/comment"
        android:maxLines="4" />

I am getting icon as left aligned but in center not at top

回答1:

  1. You have to understand what you are using. There is no such thing as android:setToTop in your editText options - that's why it does not align to the top; android:paddingTop is about something else.
  2. It depends on which layout are you using - the best one for your problem is RelativeLayout, which uses android:layout_alignTop.

I strongly suggest that you should read more about layouts which can help you understand how it works and what are you doing - copy & paste the code doesn't help every time!



回答2:

use this code

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/left_drawer"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:background="#FFA500">
    <EditText
        android:id="@+id/editComments"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:hint="Comments"
        android:inputType="textMultiLine"
        android:padding="20dp"
        android:lines="4"
        android:drawablePadding="20dp"
        android:minLines="4"
        android:gravity="top|left"
        android:maxLines="4" />
    <ImageButton
        android:layout_width="20dp"
        android:layout_gravity="left"
        android:src="@drawable/comment"
        android:layout_height="20dp" />

</FrameLayout>


回答3:

Try this,

<LinearLayout
        android:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:layout_below="@+id/rlHeader"
        android:orientation="horizontal"
        android:padding="10dp">

        <ImageView
            android:id="@+id/imgIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/icon_lock"
            android:layout_gravity="top"/>


        <EditText
            android:id="@+id/editComments"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Comments"
            android:inputType="textMultiLine"
            android:lines="4"
            android:textColor="@android:color/white"
            android:textColorHint="@android:color/white"
            android:layout_marginLeft="10dp"
            android:minLines="4"
            android:gravity="top|left"
            android:maxLines="4" />
    </LinearLayout>