Scaling an image to max available width, keeping aspect ratio and avoiding ImageView extra space

后端 未结 6 2067
后悔当初
后悔当初 2021-02-04 03:07

I have this simple layout XML, and a green_square.png file which is 30X30 pixels




        
相关标签:
6条回答
  • 2021-02-04 03:45

    In my view, this ImageView extra layout is a bug. I had the same thing happen. As a result, text that I wanted to follow the green_square followed the extra layout. So what I did was specify the text relative to the bottom of the screen. That way, the text would go over the extra layout. I admit, it wasn't the best solution, but it looks much better than having a huge space between the green_square and the text.

    0 讨论(0)
  • 2021-02-04 03:56

    Try this,

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/red"
        android:scaleType="centerCrop"
        android:src="@drawable/green_square" />
    
    </LinearLayout>
    
    0 讨论(0)
  • 2021-02-04 03:58
    <ImageView
            android:id="@+id/game"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop"
            android:adjustViewBounds="true"
            android:src="@drawable/ic_game" 
            />
    

    this works for me. Full width of parent and center crop to keep aspect ratio

    0 讨论(0)
  • 2021-02-04 03:58

    It's so simple. Just make the width fill_parent and height wrap_content. Here is the I figured it out.

    <ImageView
        android:id="@+id/game"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        android:adjustViewBounds="true"
        android:src="@drawable/ic_game" 
        />
    
    0 讨论(0)
  • 2021-02-04 04:00

    Use scale type "fitCenter" or "fitxy"

    0 讨论(0)
  • 2021-02-04 04:02

    There lots of great answers here which actually works. I just what to share the way I do this now with a more modern XML syntax, the constraint view. Trust me constraint view rooks!

    <android.support.constraint.ConstraintLayout 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"
        android:background="#394BAB"
        tools:context="com.appdomain.app.apppro.ActivityName">
    
        <ImageView
            android:id="@+id/nice_img"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="0dp"
            android:src="@drawable/green_square"
            android:scaleType="centerCrop"
            android:adjustViewBounds="true"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    
    </android.support.constraint.ConstraintLayout>
    
    

    of course the tools:context="com.collectsavings.collect.collectpro.LoginAndRegisterPrompt" and android:src="@drawable/green_square" Should all match your app activity context and drawabl resources respectively.

    0 讨论(0)
提交回复
热议问题