Image Type Android ImageView centerCrop

I am creating an action on Android. I want to show a background image. I put ImageView in my main RelativeLayout.

<RelativeLayout android:id="@+id/relativeLayoutMain" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:orientation="vertical" > <ImageView android:id="@+id/bgImage" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" android:src="@drawable/bg4" android:scaleType="centerCrop" /> </RelativeLayout> 

He pretends as follows.

enter image description here

I need to do this: enter image description here

How can i do this?

+4
source share
4 answers

Try setting the ImageView attribute ScaleType=fitEnd ?

+7
source

use this code. Issue the output as you wish.
code

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:id="@+id/relativeLayoutMain" android:layout_width="150dp" android:background="@android:color/background_dark" android:layout_height="150dp" > <ImageView android:id="@+id/bgImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:scaleType="centerCrop" android:src="@drawable/ic_launcher" /> </RelativeLayout> </RelativeLayout> 

one piece of advice, if u gives the width of the image, fills the parent object, then it contains all the space in accordance with the main container. Best luck

+1
source

Set android:background="@drawable/bg4" to RelativeLayout as follows:

All XML should look like this:

 <RelativeLayout android:id="@+id/relativeLayoutMain" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:background="@drawable/bg4 android:orientation="vertical" > </RelativeLayout> 
0
source

Firstly, I believe that there are several things in xml:

RelativeLayout does not use orientation, you must remove this

Is this your complete xml file? If so, then alignParentLeft in RelativeLayout does nothing, because it is the attribute that you use if it is the parent of RelativeLayout.

fill_parent is the deprecated name of match_parent, you should use it.

I also believe that the code you have will center the image on the screen, placing everything that is even on both sides, and not at all what you said.

If you just need a background, you can just use android:background="@drawable/bg4" in RelativeLayout

EDIT: changing the layoutView options for the wrap_content file both in height and in width and removing scaleType alltogether can make it the way you want.

0
source

Source: https://habr.com/ru/post/1492929/


All Articles