CardView上的onClick()事件



CardView中是否存在onClick((事件?

我在网上没有找到任何解决方案。

示例:

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
app:cardCornerRadius="8dp"
android:layout_marginRight="30dp"
android:layout_marginTop="30dp"
android:layout_marginLeft="30dp"
android:layout_height="80dp"/>

因此,当我点击卡片视图时,layout_height会上升到160dp。有可能吗?

您可以通过编程方式或在xml文件中设置onClick()。这是两种方式:

xml文件中:(确保您有一个名为changeHeight的方法,其中定义了参数View view(

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
app:cardCornerRadius="8dp"
android:layout_marginRight="30dp"
android:layout_marginTop="30dp"
android:layout_marginLeft="30dp"
android:layout_height="80dp"
android:onClick="changeHeight"
android:id="cardview"/>

请注意,我在此处为CardView设置了一个id。如果要以编程方式设置onClickListener(),则需要执行此操作。

程序化:

CardView cv = (CardView) findViewById(R.id.cardview); // using the id we set
// earlier.
cv.setOnClickListener(v -> {
// set the height
});

最新更新