简体   繁体   中英

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:

在此处输入图片说明

And this is the result I get:

在此处输入图片说明

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM