如何在Android屏幕上填充ImageView

时间:2018-08-06 19:47:41

标签: android android-layout android-studio

<?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>

I tried filling the screen but as you can see,the borders are black.How to fill full image?im new to android

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

4 个答案:

答案 0 :(得分:1)

将此属性添加到ImageView:

android:scaleType="centerCrop"  


android:scaleType="fitXY"

答案 1 :(得分:1)

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

 <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;

答案 2 :(得分:1)

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

 android:adjustViewBounds="true"

在xml文件中

答案 3 :(得分:0)

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

   android:scaleType="fitCenter"
相关问题