Abh*_*nda 0 android android-layout
我正在尝试执行与Android下图类似的操作。问题是,每当屏幕尺寸改变时(当用户旋转设备时),复选框和按钮就会从屏幕上消失。这是我的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/preview"
android:layout_width="fill_parent"
android:contentDescription="@string/content_description"
android:layout_height= "1.5in" />
<CheckBox
android:id="@+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/preview"
android:text="@string/location_text" />
<CheckBox
android:id="@+id/timestamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/location"
android:text="@string/timestamp_text" />
<Button
android:id="@+id/buttonUpload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/timestamp"
android:layout_marginRight="19dp"
android:gravity="center_vertical"
android:onClick="uploadClicked"
android:text="@string/upload_button_text" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
我需要显示图像和所有控件,并支持所有屏幕尺寸。如何调整ImageView动态尺寸?

我没有得到scaleType的东西为我工作。
我将ImageView子类化:
package your.pkg;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
public class AspectRatioImageView extends ImageView {
public AspectRatioImageView(Context context) {
super(context);
}
public AspectRatioImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AspectRatioImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
if (getDrawable() != null && getDrawable().getIntrinsicWidth() != 0) {
int height = width * getDrawable().getIntrinsicHeight() / getDrawable().getIntrinsicWidth();
setMeasuredDimension(width, height);
} else {
setMeasuredDimension(0, 0);
}
}
}
Run Code Online (Sandbox Code Playgroud)
并用它代替了常规的ImageView:
<your.pkg.AspectRatioImageView
android:id="@+id/photo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
/>
Run Code Online (Sandbox Code Playgroud)
这样可以缩放到父视图的整个宽度,并在这样做时保持宽高比。