【问题标题】:Place Icon at Top Left Corner in Multiline EditText将图标放在多行 EditText 的左上角
【发布时间】:2016-02-18 17:05:17
【问题描述】:

如何在多行 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" />

我的图标左对齐,但在中心而不是在顶部

【问题讨论】:

  • 您可以使用相对布局,将该图像作为您的要求

标签: android xml android-edittext drawable


【解决方案1】:
  1. 您必须了解您使用的是什么。在您的 editText 选项中没有 android:setToTop 这样的东西 - 这就是它不与顶部对齐的原因; android:paddingTopabout something else
  2. 这取决于您使用的布局 - 最适合您问题的布局是 RelativeLayout,它使用 android:layout_alignTop

我强烈建议您阅读有关 layouts 的更多信息,这可以帮助您了解它的工作原理以及您在做什么 - 复制和粘贴代码并非每次都有帮助!

【讨论】:

  • 所以最后我创建了自己的.. 显然并不难
【解决方案2】:

使用此代码

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

    试试这个,

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多