我在LinearLayout中有一个CardView。布局为CardView保留屏幕的1/5,为其他内容保留屏幕的4/5。
我希望卡片视图是正方形的。
我将CardView的高度设置为“ match_parent”。但是如何设置宽度以匹配高度?
我是否必须创建一个自定义View并使用OnMeasure(),还是有一种更简单的方法?
这是我视图的XML:
<?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"
android:orientation="vertical"
tools:context=".MainActivity">
LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4" >
<!-- Other stuff goes here-->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<androidx.cardview.widget.CardView
android:id="@+id/card_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:clickable="true"
android:foreground="?selectableItemBackground"
app:cardBackgroundColor="#81C784"
app:cardCornerRadius="12dp"
app:cardElevation="3dp"
app:contentPadding="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/card_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#000000"
android:textSize="15sp"
android:text="TEST"/>
<TextView
android:id="@+id/card_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#000000"
android:textSize="12sp"
android:text="123"/>
<ImageView
android:id="@+id/card_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:padding="3dp"
android:scaleType="fitCenter"
android:src="@mipmap/ic_launcher"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</LinearLayout>
答案 0 :(得分:0)
好吧,如果您正在寻找实现此目标的xml方法,可以使用ConstraintLayout
完成。您需要将cardView的父级布局设置为constraintlayout,而不是LinearLayout,然后设置约束和最重要的部分来设置
app:layout_constraintDimensionRatio="1:1"
就是这样,您只需要给高度和宽度指定适当的值,这将确保它始终保持1:1的纵横比。
因此您的代码应如下所示
<?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"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4" >
<!-- Other stuff goes here-->
</LinearLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!-- match_parent also works, but use 0dp in constraintLayout as below-->
<androidx.cardview.widget.CardView
android:id="@+id/card_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="5dp"
android:clickable="true"
android:foreground="?selectableItemBackground"
app:cardBackgroundColor="#81C784"
app:cardCornerRadius="12dp"
app:cardElevation="3dp"
app:contentPadding="4dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="W,1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/card_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="TEST"
android:textColor="#000000"
android:textSize="15sp" />
<TextView
android:id="@+id/card_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="123"
android:textColor="#000000"
android:textSize="12sp" />
<ImageView
android:id="@+id/card_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:padding="3dp"
android:scaleType="fitCenter"
android:src="@mipmap/ic_launcher" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
请注意,在W
之前我是如何使用1:1
来表明需要限制体重的。根据高度。
这就是上面代码的输出
另一种方法如果您不想更改约束布局,则另一种方法是像下面这样以编程方式设置它:
CardView cardView = findViewById(R.id.card_view);
ViewGroup.LayoutParams params = cardView.getLayoutParams();
params.height = params.width;
cardView.setLayoutParams(params);
所以选择最适合您的