Binev
Binev

Reputation: 276

Layout margin pushing image in ImageView

I am using grid view to display images in my app and I am using CardView for parent element. I want the CardView to have some space from the beggining of the page and between each CardView so I am setting layout_margin to be 5dp. I also want to have some space between the image and the beggining of the CardView so I put contentPadding to be 10dp. The result though is not what I want. It seems that the layout_margin is pushing the image itself and the padding for the right side is not 10dp as I want but 10dp - 5dp and I really have no clue how to fix that

Here is visual examples:

enter image description here

And this is the result I get:

enter image description here

You can see the right side of the CardView and how the image is not properly centered.

Thank you for your help!

Edit: ImageViewStyle:

    <style name="ImageViewStyle">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">@dimen/card_view_image_view_height</item>
    <item name="android:layout_gravity">center</item>
    <item name="android:layout_alignParentTop">true</item>
    <item name="android:scaleType">centerCrop</item>
</style>

Upvotes: 0

Views: 365

Answers (1)

HB.
HB.

Reputation: 4226

Try this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<android.support.v7.widget.CardView
    android:layout_width="120dp"
    android:layout_height="120dp"
    android:layout_margin="5dp"
    app:cardCornerRadius="2dp">


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:background="@drawable/image"/>


</android.support.v7.widget.CardView>

Upvotes: 2

Related Questions