【发布时间】:2016-06-12 18:22:58
【问题描述】:
我是 Android 开发的新手,在我正在开发的第一个应用程序中定位图像时遇到问题。
所以我有以下情况。
放入我拥有的布局文件中:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Dummy content. -->
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="0dp">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:src="@drawable/carbonara" />
<TextView android:id="@android:id/text1"
style="?android:textAppearanceLarge"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp" />
</LinearLayout>
</ScrollView>
所以,如您所见,有这个 ImageView 元素:
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:src="@drawable/carbonara" />
如您所见,对于这个 ImageView,我为宽度和高度设置了以下值
android:layout_width="wrap_content"
android:layout_height="300dp
这是因为我希望图像水平占据所有空间,但我还必须为高度设置一个值。
所以对于之前的值,我得到以下结果:
正如您在上一个屏幕截图中看到的那样,图像上下有一个空格,因为我手动为 ImageView 元素指定了一个特定值,这是错误的。
我已尝试设置较低的 hwight 值,并为 android:layout_height="240dp" 值删除了这个空白空间。
但我认为这不是一个好的解决方案,因为它取决于使用的屏幕。
那么,处理这种情况的最佳方法是什么?
我有一个图像视图,它必须水平填充屏幕,并且它的高度已自动处理,而不是手动指定 dp 值。
如何实现这种行为?我错过了什么?
【问题讨论】:
-
尝试把 android:scaleType="fitXy" 或 fitCenter 放到 ImageView 中。
标签: java android android-layout android-imageview android-styles