【问题标题】:Add drawable to EditText's top|left corner将drawable添加到EditText的左上角
【发布时间】:2013-07-24 07:09:57
【问题描述】:

我知道在Android中我们可以使用函数setCompoundDrawables(..)来添加drawable。

我的问题是我的 EditText 是多行的,我想将 drawable 放在 EditText 的左上角。但是使用 setCompoundDrawables() 函数,只能选择将drawable放在特定的一侧。

Android API 有可能做到吗?

【问题讨论】:

  • 你可以像这样用setCompoundDrawablesWithIntrinsicBounds()替换setCompoundDrawables(..) -> EditTextId.setCompoundDrawablesWithIntrinsicBounds(0, myIcon, 0, 0);
  • 我可以在 myIcon 中添加什么值?我知道那是 int.. 我可以在哪些值之间选择?
  • myIcon 只是一个示例,但是您可以像在 setCompoundDrawables(..) 方法中所做的那样输入相同的内容,如果您粘贴获得的代码,我可以帮助您。 :)
  • 我找到了。 int = drawble id。

标签: android android-edittext drawable


【解决方案1】:

似乎只有EditText / TextView 没有任何副作用和额外的视图是不可能的(我在最后提供了方法,但它需要更多的图像处理,并且可能不适用于每部手机,因为不同的编辑文本背景取决于制造商)。同样的问题已经被问过很多次了:例如herehere

根据我的理解,最简单和最快的方法可能不是上述问题中建议的(通过使用RelativeLayout),而只是使用FrameLayout 以下方式(你将有2个视图vs 3个视图使用@987654332 @+ImageView): 制作你的图标 9patch(使用9patch tool)。例如。您最初有以下图标:

然后你可以把它转换成下面的:

(您可以看到黑色像素,显示要拉伸的区域和内容区域)

使用以下xml:

    <!-- Main activity layout -->
    <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <!-- Edit text with drawable at top left -->
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@drawable/icon_9patch">
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/edit"
                android:hint="Enter text"
                android:maxLines="10"
                android:inputType="textMultiLine" />
        </FrameLayout>
    </RelativeLayout>

给出以下结果(我添加了多行文本):

此外,甚至可以仅使用 EditText 来执行任务(但您可能会丢失默认的 EditText 背景,这可能不好,但是您可以将其包含到您的 ning-patch 图像中;但它仍然是相同的在所有手机上,而不是本地一个)。

以下 xml 也可以正常工作:

<!-- Main activity layout -->
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- Edit text with drawable at top left -->
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/edit"
            android:hint="Enter text"
            android:maxLines="10"
            android:inputType="textMultiLine"
            android:layout_alignParentBottom="true"
            android:background="@drawable/icon_9patch" />
</RelativeLayout>

给出以下结果(注意编辑的默认框架已经消失):

【讨论】:

    【解决方案2】:

    试试这个:

               <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/scale_10"
                    android:orientation="horizontal">
    
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/map_mark"/>
    
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="@dimen/scale_10"
                        android:gravity="top"
                        android:text="TEXT HERE"/>
                </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-09
      • 1970-01-01
      • 2012-11-28
      • 1970-01-01
      • 2016-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多