Reputation: 67
I cannot get the image in the center of desktop. The image is alway on the left side. It is LinearLayout I some ScrollView.
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardCornerRadius="80dp"
app:cardElevation="10dp"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/titelbild280x280"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
</android.support.v7.widget.CardView>
Upvotes: 5
Views: 8684
Reputation: 1
The easiest way that worked for me was using relative layout inside my cardView and then using the android:layout_centerInParent="true". This works perfectly when the width and height of the of the relative layout are match parent (Parent being the cardView).
Upvotes: 0
Reputation: 9225
Please try following code:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardCornerRadius="10dp"
app:cardElevation="10dp"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/titelbild280x280"
android:layout_gravity="center"/>
</android.support.v7.widget.CardView>
</LinearLayout>
I hope its works for you.
Upvotes: 0
Reputation: 61
You have to use a layout (ConstraintLayout
or RelativeLayout
or LinearLayout
) for managing multiple view in card view
Currently android UI system deeply coupled with ConstraintLayout
for handling complex layout and making it easily, so following codes maybe help to you
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardCornerRadius="80dp"
app:cardElevation="10dp"
android:gravity="center"
android:orientation="vertical">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/titelbild280x280"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
Thanks.
Upvotes: 2
Reputation: 4494
Simply try this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/some_img" />
</androidx.cardview.widget.CardView>
Upvotes: 0
Reputation: 153
Try this one Hope it will work for you
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardCornerRadius="80dp"
app:cardElevation="10dp"
android:gravity="center"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:orientation="vertical">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/featureimage"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:src="@drawable/image"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
Upvotes: 0
Reputation: 6495
CardView is an extension of FrameLayout. So you should use the same layout values as you would in a FrameLayout.
To center a child view you use:
android:layout_gravity="center"
So your layout becomes:
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardCornerRadius="80dp"
app:cardElevation="10dp"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/titelbild280x280"
/>
</android.support.v7.widget.CardView>
However, note that the CardView in this example uses wrap_content, so it will firmly wrap the image and there will be nothing left to center. For the gravity to have effect the CardView has to be larger than the ImageView.
Upvotes: 8