繁体   English   中英

如何在Android屏幕上填充ImageView

[英]How do you fill imageview to screen in android

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="galleryimages.galleryimages.Main2Activity"
    android:background="#000000"
    android:id="@+id/linear1">


    <ImageView
        android:id="@+id/iv_image2"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_above="@+id/button1"
        android:layout_weight="0"
        android:layout_gravity="center"
        tools:ignore="InvalidId" />


    <ImageButton
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal"
        android:layout_weight="1"
        android:adjustViewBounds="false"
        android:background="#ffffff"
        android:gravity="bottom"
        android:src="@drawable/ic_delete_black_24dp" />


</RelativeLayout>

我尝试填充屏幕,但如您所见,边框为黑色。如何填充完整图像?

我希望图像充满整个屏幕。 我应该为18:9比例的手机编写单独的代码吗? 另外,如果我删除图像,则不会与Google照片同步。

将此属性添加到ImageView中:

android:scaleType="centerCrop"  

要么

android:scaleType="fitXY"

您可以从以下代码中使用:

 <ImageView
                android:id="@+id/img"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_gravity="center"
                android:layout_marginBottom="64dp"
                android:adjustViewBounds="true" />

和其他方式:

DisplayMetrics displayMetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
            int height = displayMetrics.heightPixels;
            int width = displayMetrics.widthPixels;
            int ratio = imgTop.getDrawable().getIntrinsicWidth() / imgTop.getDrawable().getIntrinsicHeight();
            int newheght = width / ratio;
            imgTop.getLayoutParams().height = newheght;

我认为您必须添加以下代码:

 android:adjustViewBounds="true"

在xml文件中

这总是对我有用。 将其添加到ImageView属性中:

   android:scaleType="fitCenter"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM