使用ImageView旋转Android活动会将ImageView缩小到1px
我有一个活动,用户可以通过意图选择照片。预期的行为如下所示:使用ImageView旋转Android活动会将ImageView缩小到1px,android,layout,rotation,imageview,Android,Layout,Rotation,Imageview,我有一个活动,用户可以通过意图选择照片。预期的行为如下所示: 图像大小调整为填充父宽度(减去边距) 这种大小调整不应增加图像,只应在需要时缩小 纵横比应始终准确 高度应精确缩放为宽度,以保持正确的纵横比 在纵向模式下,最大高度是在滚动显示之前可以达到的最大高度(只需填充屏幕) 这意味着,如果受高度限制,宽度可能不会完全延伸到父宽度 在横向模式下,最大高度不相关,因为允许滚动,宽度应始终为父宽度 下面是我正在使用的纵向XML布局: <LinearLayout xmlns:andro
- 图像大小调整为填充父宽度(减去边距)李>
- 这种大小调整不应增加图像,只应在需要时缩小
- 纵横比应始终准确李>
- 高度应精确缩放为宽度,以保持正确的纵横比李>
- 在纵向模式下,最大高度是在滚动显示之前可以达到的最大高度(只需填充屏幕)
- 这意味着,如果受高度限制,宽度可能不会完全延伸到父宽度
- 在横向模式下,最大高度不相关,因为允许滚动,宽度应始终为父宽度
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/gray_background"
android:weightSum="1" >
<!-- Other controls here -->
<ImageView android:id="@+id/imgView"
android:layout_gravity="center"
android:layout_margin="5dip"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:contentDescription="@string/ImageToSend"
android:adjustViewBounds="true"
android:layout_weight="1"
android:scaleType="fitCenter" />
<!-- Other controls here -->
</LinearLayout>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<!-- Other controls are here -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/gray_background" >
<ImageView android:id="@+id/imgView"
android:layout_gravity="center"
android:layout_margin="5dip"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:contentDescription="@string/ImageToSend"
android:adjustViewBounds="true"
android:layout_weight="1"
android:scaleType="fitCenter" />
<!-- Other controls are here -->
</LinearLayout>
</ScrollView>
以下是我正在使用的横向XML布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/gray_background"
android:weightSum="1" >
<!-- Other controls here -->
<ImageView android:id="@+id/imgView"
android:layout_gravity="center"
android:layout_margin="5dip"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:contentDescription="@string/ImageToSend"
android:adjustViewBounds="true"
android:layout_weight="1"
android:scaleType="fitCenter" />
<!-- Other controls here -->
</LinearLayout>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<!-- Other controls are here -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/gray_background" >
<ImageView android:id="@+id/imgView"
android:layout_gravity="center"
android:layout_margin="5dip"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:contentDescription="@string/ImageToSend"
android:adjustViewBounds="true"
android:layout_weight="1"
android:scaleType="fitCenter" />
<!-- Other controls are here -->
</LinearLayout>
</ScrollView>
无论我持何种观点,这都非常有效。我遇到的问题是,当用户旋转设备时,ImageView不再具有正确的大小。如果我以横向模式选择图像,旋转看起来很好。如果我从纵向模式选择图像开始,然后切换到横向模式,图像现在显示为1px x x 1px
有什么想法吗?在一段时间的过期失败后,我发现调用setContentView()并重新进行布局解决了这个问题。不确定这是否是我应该做的,但它已经解决了