Issue
I am absolutly new in Android development and I have a problem positioning image in the first app that I am developing.
So I have the following situation.
Into a layout file I have:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Dummy content. -->
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="0dp">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:src="@drawable/carbonara" />
<TextView android:id="@android:id/text1"
style="?android:textAppearanceLarge"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp" />
</LinearLayout>
</ScrollView>
So, as you can see, there is this ImageView element:
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:src="@drawable/carbonara" />
As you can see, for this ImageView I have setted the following values for the width and for the height
android:layout_width="wrap_content"
android:layout_height="300dp
This because I want that the image occupies horizontally all the space, but I have also to put a vaue for the height.
So for the previous value I obtain the following result:
As you can see in the previous screenshot there is a space up and down the image because I am manually giving a specific value to my ImageView element and this is wrong.
I have tryed to set a lower hwight value and this empty space is deleted for the android:layout_height="240dp" value.
But I think that this is not a good solution because it depens from the used screen.
So, what is the best way to handle this situation?
I have an image view that horizontally have to fill the screen and its height have be automatically handled and not manually specified with a dp value.
How can I implement this behavior? What am I missing?
Solution
Your problem can be solved with this simple line of code below
The XML attribute for this is:
android:scaleType="fitCenter"
Put it here like this your code below I just added new line see #1
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:scaleType="fitCenter" // #1
android:layout_height="300dp"
android:src="@drawable/carbonara" />
Also you are using src but you can use background attribute:
android:background="@drawable/carbonara"
Answered By - Himanshu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.