如何制作CardView
正方形。
当我在layout_weight
上设置CardView
时,它并没有像CardView
上的正方形一样设置。
请给我一个解决方案:(不设置固定的高度和宽度)
代码:
<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>
答案 0 :(得分:6)
您可以使用ConstraintLayout
代替权重的多个LinearLayout
。
添加
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
在您的build.gradle
中。然后您可以尝试类似的操作(将DimensionRatio设置为1):
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp">
<android.support.v7.widget.CardView
android:id="@+id/card1"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/no_picture" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/card2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="4dp"
app:layout_constraintBottom_toTopOf="@id/card3"
app:layout_constraintDimensionRatio="1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/card1"
app:layout_constraintTop_toTopOf="@id/card1">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/no_picture" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/card3"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="4dp"
app:layout_constraintBottom_toBottomOf="@id/card1"
app:layout_constraintDimensionRatio="1"
app:layout_constraintEnd_toEndOf="@id/card2"
app:layout_constraintStart_toStartOf="@id/card2"
app:layout_constraintTop_toBottomOf="@id/card2">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/no_picture" />
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
结果:
答案 1 :(得分:0)
您可以使用自定义CardView,并覆盖onMeasure()以强制采用1:1的宽高比
public class SquareCardView extends CardView {
public SquareCardView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int ignoredHeightMeasureSpec) {
int newHeightMeasureSpec = widthMeasureSpec;
super.onMeasure(widthMeasureSpec, newHeightMeasureSpec);
}
}
然后在您的xml中使用此代码
<com.example.view.SquareCardView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<ImageView
android:src="@drawable/square1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/main_dark"
android:adjustViewBounds="true" />
</com.example.view.SquareCardView>