添加绘制到的EditText顶级|左上角 [英] Add drawable to EditText's top|left corner

查看:387
本文介绍了添加绘制到的EditText顶级|左上角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know that in Android we can use function setCompoundDrawables(..) to add drawable.

我的问题是,我的EditText是MULTY线和我希望把绘制在EditText上顶尖|左上角。但在setCompoundDrawables使用()函数,只给选择将绘制在特定的身边。

My problem is that my EditText is multy-lines and i want to put drawable in EditText's top|left corner. But using at setCompoundDrawables() function, only give choice to put drawable in specific side.

时它可能在Android的API来做到这一点?

Is it possibility at Android API to Do it?

推荐答案

似乎是不可能的,只有做到这一点的EditText / 的TextView 无anyside效果和附加的意见(我提供的方式在最后,但它需要根据,因为对manufature不同的编辑文本backgorunds更多的工作,形象,可以不适用于每一个电话,)。同样的问题也被提出已经有很多次:这里和<一href="http://stackoverflow.com/questions/14726583/how-to-place-compound-drawable-on-top-left-of-text-view">here例如,

Seems it's impossible to do it with only EditText / TextView without anyside effect and additional views (I've provided the way at the end, but it needs more work with image and can be not applicable for every phone, because of different edit text backgorunds depending of manufature). The same question have been asked already many times: here and here for example.

按我的理解是最简单和fastests的方式可能不一样建议在上述问题(由 RelativeLayout的用法),但只使用的FrameLayout 通过以下方式(你将有2次对3视图使用 RelativeLayout的 + 的ImageView ) : 让你的图标9patch(使用 9patch工具)。例如。您有以下图标的,最初:

Per my understanding the easiest and fastests way might be not as suggested in above questions (by usage of RelativeLayout), but just using FrameLayout the following way (You'll have 2 views vs 3 view using RelativeLayout+ImageView): Make your icon 9patch (using 9patch tool). E.g. you have the following icon initially:

然后你可以将其转换为下列之一:

Then You can convert it to the following one:

(你可以看到黑色像素,显示面积与视图和领域被拉长内容)

(You can see black pixels which shows area to be stretched with view and areas for content)

使用以下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>

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

Gives the following result (I've added multiple lines of text):

此外,它遵从任务与甚至有可能只的EditText (但你可能会失去默认的EditText背景,可能不是很好,但可以将其包含到宁 - 修补形象,但它仍然会在所有的手机,而不是相同的本机的一个)

Moreover, it's even possible to comply the task with only EditText (but You might loose default EditText background which might not be good, however You can include it into your ning-patch image; but it will still be the same on all phones instead of native one).

下面的XML也能正常工作:

The following xml also works fine:

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

给出的结果(请注意,默认情况下帧的编辑已经消失了):

Give the following result (note that default frame for edit has disappeared):

这篇关于添加绘制到的EditText顶级|左上角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆