Android 将图标放置在多行编辑文本的左上角

Android 将图标放置在多行编辑文本的左上角,android,xml,android-edittext,drawable,Android,Xml,Android Edittext,Drawable,如何将图标放置在多行EditText的左上角,以下是我使用的内容: <EditText android:id="@+id/editComments" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="10dp" android:paddingTop="10dp"

如何将图标放置在多行EditText的左上角,以下是我使用的内容:

<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" />

我得到的图标是左对齐的,但在中间而不是顶部使用此代码

<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>

  • 你必须明白你在用什么。在你的editText选项中没有android:setToTop这样的东西——这就是为什么它不与顶部对齐;android:paddingTop
  • 这取决于您使用的布局-解决问题的最佳布局是
    RelativeLayout
    ,它使用
    android:layout\u alignTop
  • 我强烈建议你应该多读一些有助于你理解它是如何工作的以及你在做什么的东西——复制和粘贴代码并不是每次都有用

    试试这个

    <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>
    
    
    
    您可以使用相对布局,将该图像作为您的要求放置,因此我最终创建了自己的。。显然这并不难