【问题标题】:Android: how to get ImageView to fill screenAndroid:如何让 ImageView 填满屏幕
【发布时间】:2016-04-25 19:45:05
【问题描述】:

我不知道如何让图像填满屏幕,已经尝试了所有方法,也许我应该使用一些特定的布局..?

我使用的是 ImageView,但它只占了屏幕的 70%,底部留有空白...

目前我只是使用 LinearLayout 并将其背景设置为我的图像。虽然这会填满屏幕,但图像会略微拉伸,这是我不想要的。

任何帮助将不胜感激! :)

我的布局文件的 ImageView 标签内的代码是:

android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:scaleType="centerCrop" 
android:adjustViewBounds="true" 
android:src="@drawable/image" 

【问题讨论】:

  • android:layout_height="match_parent" 会做

标签: android layout


【解决方案1】:

//在你的xml图片属性中为

android:scaleType="fitXY"

或 //以编程方式使用

imgview.setScaleType(ScaleType.FIT_XY);

注意:这适用于图像设置为 android:src="..." 而不是 android:background="..." 因为背景默认设置为拉伸和适合视图。

【讨论】:

  • 当你说通过你的编码也使用它时,你的意思是在你的活动中吗?
  • @Shaw 不,我的意思是你也可以通过程序做到这一点
  • 这个答案将拉伸图像 - 我知道这不是 OP 的要求,但如果你想要一个在不破坏纵横比的情况下填满屏幕的答案,请查看下面的 @Revs 答案。跨度>
【解决方案2】:
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop"
    android:adjustViewBounds="true"
    android:src="@drawable/image"

只需更改适用于“中心”和“中心裁剪”的布局宽度和高度

【讨论】:

  • 不知道为什么这不被接受 - 这个答案在不拉伸图像和丢失正确的纵横比的情况下有效。
  • 这是完美的答案
【解决方案3】:

在您的 Image View xml 代码中使用以下行。

android:scaleType="fitXY"

【讨论】:

  • 可能会,但它会按照 OP 的要求填满屏幕,如果图像分辨率足够好,那么它应该不会出现问题。
【解决方案4】:

别忘了删除

android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"

来自layoutparams,位于活动的xml顶部。 否则图片将无法填满整个屏幕。

【讨论】:

    【解决方案5】:

    您几乎可以使用任何布局,只需将背景图像设置为您的图像。然后让宽度和高度填充父级。

    或者例如,如果您只想要一个图像,则为您的主要线性布局设置它

        <LinearLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:orientation="horizontal"
          android:layout_height="fill_parent"
          android:background="@drawable/yourimage">
    </LinearLayout>     
    

    【讨论】:

      猜你喜欢
      • 2011-01-24
      • 1970-01-01
      • 2015-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多