ImageView adjustViewBounds 不适用于相对布局
ImageView adjustViewBounds not working with Relative Layout
我有一个 ImageView
并将其 layout_height
设置为“100dp”,并将其 layout_width 设置为 "wrap_content"。
使用的 drawable 具有更大的实际尺寸,为 130dp(宽度)X 215dp(高度)。
当 ImageView
放置在 LinearLayout
内时,如果我将 adjustViewBounds
设置为 true,它会根据 drawable 的纵横比和 layout_width.
但是,当放置在 RelativeLayout
中时,无论 adjustViewBounds 设置如何,ImageView
都会获得与可绘制对象相同的宽度 (130dp)。
在这两种情况下,图像都呈现正确且没有失真,但是 ImageView 的边界不同。
相对布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_light">
<ImageView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:adjustViewBounds="true"
android:src="@drawable/phone_orange"
android:contentDescription="@null" />
</RelativeLayout>
线性布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_light"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:adjustViewBounds="true"
android:contentDescription="@null"
android:src="@drawable/phone_orange" />
</LinearLayout>
为什么 ImageView
在这两种情况下的测量方式不同?有没有办法让 ImageView
的行为总是像在 LinearLayout 的情况下一样(用 LinearLayout 包装它可以,但它不是一个干净的解决方案)?
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxHeight="100dp"
android:adjustViewBounds="true"
I got it there
使用 imageview 的宽度和高度 match_parent
private void addView(Drawable d) {
RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
ImageView iv = new ImageView(this);
iv.setImageDrawable(d);
iv.setAdjustViewBounds(true);
iv.setScaleType(ImageView.ScaleType.MATRIX);
iv.setLayoutParams(lparams);
rl.addView(iv);
iv.setOnTouchListener(this);
}
我有一个 ImageView
并将其 layout_height
设置为“100dp”,并将其 layout_width 设置为 "wrap_content"。
使用的 drawable 具有更大的实际尺寸,为 130dp(宽度)X 215dp(高度)。
当 ImageView
放置在 LinearLayout
内时,如果我将 adjustViewBounds
设置为 true,它会根据 drawable 的纵横比和 layout_width.
但是,当放置在 RelativeLayout
中时,无论 adjustViewBounds 设置如何,ImageView
都会获得与可绘制对象相同的宽度 (130dp)。
在这两种情况下,图像都呈现正确且没有失真,但是 ImageView 的边界不同。
相对布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_light">
<ImageView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:adjustViewBounds="true"
android:src="@drawable/phone_orange"
android:contentDescription="@null" />
</RelativeLayout>
线性布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_light"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:adjustViewBounds="true"
android:contentDescription="@null"
android:src="@drawable/phone_orange" />
</LinearLayout>
为什么 ImageView
在这两种情况下的测量方式不同?有没有办法让 ImageView
的行为总是像在 LinearLayout 的情况下一样(用 LinearLayout 包装它可以,但它不是一个干净的解决方案)?
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxHeight="100dp"
android:adjustViewBounds="true"
I got it there
使用 imageview 的宽度和高度 match_parent
private void addView(Drawable d) {
RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
ImageView iv = new ImageView(this);
iv.setImageDrawable(d);
iv.setAdjustViewBounds(true);
iv.setScaleType(ImageView.ScaleType.MATRIX);
iv.setLayoutParams(lparams);
rl.addView(iv);
iv.setOnTouchListener(this);
}