簡體   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