我想降低ImageView的高度和TextView的高度,并保持它的纵横比。图像的实际尺寸为:128 * 128
这是我的代码:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#b6006a"
android:gravity="center_horizontal"
android:orientation="horizontal">
<ImageView
android:id="@+id/ImageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxHeight="10dp"
android:minHeight="40dp"
android:maxWidth="40dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:padding="5dip"
android:scaleType="fitXY"
android:src="@drawable/icon" />
<TextView
android:id="@+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#b6006a"
android:padding="10dip"
android:text="New Message"
android:textColor="#fff"
android:textStyle="bold"
android:textSize="20dp" />
</LinearLayout>
有什么建议吗?
答案 0 :(得分:0)
您可以使用此代码在运行时调整图像大小。
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,int reqWidth, int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
答案 1 :(得分:-1)
使用RelativeLayout:
机器人:paddingBottom来= “3dip”
机器人:重力= “CENTER_HORIZONTAL”
机器人:取向= “水平” &GT;
<ImageView
android:id="@+id/ImageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="40dp"
android:paddingTop="2dip"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@drawable/icon" />
<TextView
android:id="@+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#b6006a"
android:padding="10dip"
android:layout_marginLeft="30dp"
android:text="New Message"
android:textColor="#fff"
android:textStyle="bold"
android:textSize="20dp"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/ImageView1"
/>