在多行 EditText 的左上角放置图标
Place Icon at Top Left Corner in Multiline EditText
如何在多行 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:padding顶部是about something else。
- 这取决于您使用的布局 - 最适合您问题的布局是
RelativeLayout
,它使用 android:layout_alignTop
。
我强烈建议您阅读更多有关 layouts 的内容,这可以帮助您了解它的工作原理以及您在做什么 - 每次复制和粘贴代码都无济于事!
试试这个,
<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>
如何在多行 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:padding顶部是about something else。
- 这取决于您使用的布局 - 最适合您问题的布局是
RelativeLayout
,它使用android:layout_alignTop
。
我强烈建议您阅读更多有关 layouts 的内容,这可以帮助您了解它的工作原理以及您在做什么 - 每次复制和粘贴代码都无济于事!
试试这个,
<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>