0

I am trying to do something similar to the image below in Android. The problem is, whenever the screen size changes (when the user rotates the device), the checkboxes and the button disappear from the screen. This is my layout

<?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>

I need to display the image and all controls and support all screen sizes. How do I make the size of the ImageView dynamic?

shot

4

3 回答 3

5

我从来没有让 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);
  }

 }
}

并使用它而不是常规的 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"
/>

这会缩放到父视图的整个宽度,并在这样做时保持纵横比。

于 2012-05-26T23:55:05.173 回答
1

如果您使用 aLinearLayout而不是 a RelativeLayout,则可以进行ImageView扩展以占用屏幕中的所有空白空间。该scaleType属性将让您指定如何拉伸图像。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
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="0dp"
android:layout_weight="1"
android:scaleType="fitCenter" />

<CheckBox
android:id="@+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/location_text" />

<CheckBox
android:id="@+id/timestamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/timestamp_text" />

<Button
android:id="@+id/buttonUpload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="19dp"
android:layout_gravity="right"
android:onClick="uploadClicked"
android:text="@string/upload_button_text" />

</LinearLayout>
于 2012-05-26T23:15:33.657 回答
0

基于所有反馈。这是我使用的:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroller"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >
    <RelativeLayout
        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>
</ScrollView>
于 2012-05-27T00:17:21.727 回答