Make ImageView fit width of CardView

前端 未结 10 672
太阳男子
太阳男子 2020-12-04 11:00

I have a CardView with rounded corners, I want to have an ImageView at the top like shown in the example taken from the material design guidelines

相关标签:
10条回答
  • 2020-12-04 11:49

    I solved the problem with setting
    1) app:cardUseCompatPadding="false"
    2) setting a rounded imageView background

    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <corners android:radius="4dp" />
    </shape>
    
    0 讨论(0)
  • 2020-12-04 11:52

    I got this to work by putting a RoundedImageView inside of a CardView. Also you need to set the appropriate CardView attributes.

    https://medium.com/@etiennelawlor/layout-tips-for-pre-and-post-lollipop-bcb2e4cdd6b2#.kmb24wtkk

    0 讨论(0)
  • 2020-12-04 11:53

    i try to using combination with cardview and imageview, so it will be like this :

        android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="160dp"
                android:layout_marginStart="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="16dp"
                android:layout_marginRight="16dp"
                app:cardCornerRadius="8dp"
                android:elevation="10dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView">
    
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/kmp_warna1"
                    android:scaleType="centerCrop"/>
    
      </android.support.v7.widget.CardView>
    

    I just adding the corner radius and elevation attribute on cardview, and scaletype = centerCrop on image view

    Hope it will help

    0 讨论(0)
  • 2020-12-04 11:54

    Make a bck_rounded.xml in drawable folder. Give it a radius that is same as the card_view.

    <?xml version="1.0" encoding="utf-8"?>
        <shape xmlns:android="http://schemas.android.com/apk/res/android" >
            <corners android:radius="4dp" />
        </shape>
    

    Apply inside your imageView: android:background="@drawable/bck_rounded"

    0 讨论(0)
提交回复
热议问题