【问题标题】:How can I correctly put an image at the top of an ImageView having a specified height?如何正确地将图像放在具有指定高度的 ImageView 顶部?
【发布时间】:2016-06-21 09:38:17
【问题描述】:

我绝对是 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>

因此,正如您在前面的代码 sn-p 中看到的,我将背景图像设置为 ImageView,并设置了 350dp 的高度。

问题是图像高度小于 350dp,这就是我得到的:

正如您在上一个屏幕截图中看到的那样,我得到的是 350dp ImageView(以蓝色突出显示),并且图像在其中设置为垂直居中。我不希望设置的图像垂直居中,但我希望它从 ImageView 的顶部开始。

如何实现这种行为?我错过了什么?

我尝试过改变:

android:src="@drawable/carbonara"/>

与:

android:background="@drawable/carbonara" />

并以这种方式填充所有 ImageView 空间,但这样图像可能会出现变形,所以我认为这不是一个好的解决方案。

【问题讨论】:

    标签: java android android-layout android-imageview android-image


    【解决方案1】:

    尝试更改布局并再次测试。

    android:scaleType="fitCenter"
    

    android:scaleType="fitStart"
    

    在此处查看完整的docs

    【讨论】:

    • Tnx 完美,ant tnx 供文档参考
    猜你喜欢
    • 2016-01-16
    • 1970-01-01
    • 2018-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-21
    相关资源
    最近更新 更多