我绝对是 Android 开发的初学者,在将图像定位到布局的 ImageView 元素中时遇到一些问题。所以我有以下情况,我有这个 fragment 布局:
<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:scaleType="fitCenter"
android:layout_height="350dp"
android:src="@drawable/carbonara"/>
<!--android:background="@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>
因此,正如您在前面的代码 fragment 中看到的那样,我将背景图像设置到 ImageView 中,设置了高度为 350dp。
问题是图像高度小于 350dp,这就是我得到的:
正如您在前面的屏幕截图中看到的,我获得的是 350dp ImageView (以蓝色突出显示),并且图像设置在其中垂直居中。我不希望设置的图像垂直居中,但我希望它从 ImageView 的顶部开始。
我该如何实现这种行为?我错过了什么?
我尝试过改变:
android:src="@drawable/carbonara"/>
与:
android:background="@drawable/carbonara" />
这样它会填充所有的ImageView空间,但这样图像可能会出现变形,所以我认为这不是一个好的解决方案。
最佳答案
关于java - 如何正确地将图像放置在具有指定高度的 ImageView 的顶部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37940772/