[英]How to make square CardView (Android)
How to make square CardView
. 如何制作方形
CardView
。
When I set the layout_weight
on CardView
it is not set like square on CardView
. 当我在
CardView
上设置layout_weight
,它的设置不像CardView
上的CardView
。
Please give me a solution : (without set the fixed height and width) 请给我一个解决方案:(不设置固定的高度和宽度)
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>
You can use a ConstraintLayout
instead of multiple LinearLayout
with weight. 您可以使用
ConstraintLayout
代替带LinearLayout
重的多个LinearLayout
。
Add 加
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
in your build.gradle
. 在您的
build.gradle
。 Then you can try something like that (with dimensionRatio set to 1): 然后,您可以尝试类似的操作(将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>
The result: 结果:
you could use a custom CardView, and override onMeasure() to force a 1:1 aspect ratio 您可以使用自定义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);
}
}
Then use this code in your xml 然后在您的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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.