编辑框(EditText)左侧视图的图片如何适应编辑框大小(Android)

6

我想在EditText里放一个图标,所以我这样做:

android:drawableLeft="@drawable/search_icon"

问题在于图片比EditText大,因此EditText会变大以适应图片大小。我想要的是相反的情况:图片应该调整大小以适应EditText的高度,这可能吗?
我(拼命地)尝试了以下方法:
android:adjustViewBounds="true"

但显然这样并不管用。
有没有什么方法可以做到呢?

android:layout_height="wrap_content" 的意思是“自适应高度”。 - sasikumar
@sasikumar 它已经设置为wrap_content了,我还尝试设置自定义高度,但图像被裁剪了。 - LS_
嗨,你想要一个幻觉方法吗? - Sheychan
@Sheychan 如果它允许我将OnClickListener设置到leftView上,当然可以! - LS_
啊呼,让我来构建一些东西。 - Sheychan
edittext.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.arrow_right_normal,0); 尝试一下。 - sasikumar
3个回答

2

尝试

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#dd80aa"
android:orientation="vertical">

<RelativeLayout
    android:layout_width="363dp"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter text here"
        android:marginLeft="50dp"/>

    <ImageView
        android:layout_width="wrap_contenta"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_gravity="right"
        android:src="@android:drawable/ic_menu_search"/>

</RelativeLayout>


</LinearLayout>

0

你可以使用RelativeLayout进行包裹,在左侧设置一个layout_height为match_parent的ImageView


0

您可以通过固定编辑文本的高度来轻松实现此操作。

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:ems="10"
    android:drawableLeft="@drawable/beautiful"
    android:inputType="textPersonName" >

    <requestFocus />

</EditText>

您可以使用以下代码进行调整大小

Drawable d = imageView.getDrawable();
    Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
    byte[] ba;
    do {
        ByteArrayOutputStream bao = new ByteArrayOutputStream();

        Log.e("BEFORE REDUCING",
                bitmap.getHeight() + " " + bitmap.getWidth() + " "
                        + bitmap.getRowBytes() * bitmap.getHeight());

        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bao);
        Log.e("After REDUCING",
                bitmap.getHeight() + " " + bitmap.getWidth() + " "
                        + bitmap.getRowBytes() * bitmap.getHeight());
        ba = bao.toByteArray();
        if ((ba.length / 1024) >= 650) {
            bitmap = Bitmap.createScaledBitmap(bitmap,
                    (int) (bitmap.getWidth() * 0.95),
                    (int) (bitmap.getHeight() * 0.95), true);

        }

        Log.e("BYTE LENGTH", "" + ba.length / 1024);

    } while ((ba.length / 1024) >= 650);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接