EditText leftView 图像适合 EditText 的大小 Android
EditText leftView image fit the size of the EditText Android
我想在 EditText
中放置一个图标,所以我这样做了:
android:drawableLeft="@drawable/search_icon"
问题是图像比 EditTex
t 大,所以 EditText
变大以适应图像大小。我想要的是相反的:图像应该调整大小以适应 EditText
高度,这可能吗?
我(绝望地)尝试过:
android:adjustViewBounds="true"
但是显然不行
有什么办法吗?
您只需固定编辑文本的高度即可。
<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);
你可以包裹在 RelativeLayout 中,在左边设置一个 ImageView layout_height match_parrent
尝试
<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>
我想在 EditText
中放置一个图标,所以我这样做了:
android:drawableLeft="@drawable/search_icon"
问题是图像比 EditTex
t 大,所以 EditText
变大以适应图像大小。我想要的是相反的:图像应该调整大小以适应 EditText
高度,这可能吗?
我(绝望地)尝试过:
android:adjustViewBounds="true"
但是显然不行
有什么办法吗?
您只需固定编辑文本的高度即可。
<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);
你可以包裹在 RelativeLayout 中,在左边设置一个 ImageView layout_height match_parrent
尝试
<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>