How to make square CardView
.
When I set the layout_weight
on CardView
it is not set like square on CardView
.
Please give me a solution : (without set the fixed height and width)
See picture below:
Code:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:weightSum="3">
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:layout_weight="2"
app:cardBackgroundColor="@color/silver"
tools:ignore="NestedWeights">
</android.support.v7.widget.CardView>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:weightSum="1">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:layout_weight="0.5"
app:cardBackgroundColor="@color/silver">
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:layout_weight="0.5"
app:cardBackgroundColor="@color/silver">
</android.support.v7.widget.CardView>
</LinearLayout>
</LinearLayout>
Pycpik :
You can use a ConstraintLayout instead of multiple LinearLayout with weight.\n\nAdd\n\nimplementation 'com.android.support.constraint:constraint-layout:1.1.2'\n\n\nin your build.gradle. Then you can try something like that (with dimensionRatio set to 1):\n\n<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<android.support.constraint.ConstraintLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\nxmlns:app=\"http://schemas.android.com/apk/res-auto\"\nandroid:layout_width=\"match_parent\"\nandroid:layout_height=\"match_parent\"\nandroid:padding=\"4dp\">\n\n<android.support.v7.widget.CardView\n android:id=\"@+id/card1\"\n android:layout_width=\"0dp\"\n android:layout_height=\"0dp\"\n app:layout_constraintDimensionRatio=\"1\"\n app:layout_constraintStart_toStartOf=\"parent\"\n app:layout_constraintTop_toTopOf=\"parent\">\n\n <ImageView\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\"\n android:src=\"@drawable/no_picture\" />\n</android.support.v7.widget.CardView>\n\n<android.support.v7.widget.CardView\n android:id=\"@+id/card2\"\n android:layout_width=\"0dp\"\n android:layout_height=\"0dp\"\n android:layout_marginStart=\"4dp\"\n app:layout_constraintBottom_toTopOf=\"@id/card3\"\n app:layout_constraintDimensionRatio=\"1\"\n app:layout_constraintEnd_toEndOf=\"parent\"\n app:layout_constraintStart_toEndOf=\"@id/card1\"\n app:layout_constraintTop_toTopOf=\"@id/card1\">\n\n <ImageView\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\"\n android:src=\"@drawable/no_picture\" />\n</android.support.v7.widget.CardView>\n\n\n<android.support.v7.widget.CardView\n android:id=\"@+id/card3\"\n android:layout_width=\"0dp\"\n android:layout_height=\"0dp\"\n android:layout_marginTop=\"4dp\"\n app:layout_constraintBottom_toBottomOf=\"@id/card1\"\n app:layout_constraintDimensionRatio=\"1\"\n app:layout_constraintEnd_toEndOf=\"@id/card2\"\n app:layout_constraintStart_toStartOf=\"@id/card2\"\n app:layout_constraintTop_toBottomOf=\"@id/card2\">\n\n <ImageView\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\"\n android:src=\"@drawable/no_picture\" />\n</android.support.v7.widget.CardView>\n\n</android.support.constraint.ConstraintLayout>\n\n\nThe result:\n\n",
2018-07-20T08:11:03