简体   繁体   English

布局边距在ImageView中推动图像

[英]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. 我正在使用网格视图在应用程序中显示图像,并且正在使用CardView作为父元素。 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. 我希望CardView在页面的开头和每个CardView之间都有一些空间,因此我将layout_margin设置为5dp。 I also want to have some space between the image and the beggining of the CardView so I put contentPadding to be 10dp. 我还想在图像和CardView的开始之间留一些空间,因此我将contentPadding设置为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 似乎layout_margin正在推动图像本身,右侧的填充不是我想要的10dp,而是10dp-5dp,我真的不知道如何解决该问题

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. 您可以看到CardView的右侧以及图像未正确居中的方式。

Thank you for your help! 谢谢您的帮助!

Edit: ImageViewStyle: 编辑: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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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