Android 删除立面后,CardView将占用立面的空间
当我降低CardView的标高时,它占用了标高的空间Android 删除立面后,CardView将占用立面的空间,android,android-layout,android-cardview,Android,Android Layout,Android Cardview,当我降低CardView的标高时,它占用了标高的空间 这是正常的图片:- 这是当单击时:- 再次单击时,我会尝试将其高程设置回正常值:- 点击的项目为(1,2) 这是我的cardView代码:- <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" android:id="@+id/card_view_categories"
这是正常的图片:-
这是当单击时:- 再次单击时,我会尝试将其高程设置回正常值:- 点击的项目为(1,2) 这是我的cardView代码:-
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view_categories"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="2dp"
card_view:cardBackgroundColor="#fffff7"
card_view:cardCornerRadius="0dp"
card_view:cardUseCompatPadding="true"
设置
视图
的仰角或平移Z而不是CardView
将对您有所帮助
public boolean press=false;
cardviewrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!press){
v.setTranslationZ(50);
//or you can use
v.setElevation(50);
press=false;
}else{
v.setTranslationZ(0);
//or you can use
v.setElevation(0);
press=true;
}
}
});
如果您不想收到有关最低Api级别的警告,请使用以下命令:
if(isLolipop()) v.setTranslationZ(50);
public static boolean isLolipop(){
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
}
你能发布你的
cardwiew
xml代码吗?实际上我有cardwiew
,我可以通过编程设置高程,并且尺寸相同,只是高程在变化。把你的onClick
代码贴出来,我来看看。我有两个解决方案。实际上,我的onclick方法与您相同,但我使用的是view.setElevation
而不是card.setElevation
,首先您可以试试这个方法。@YasinKaçmaz很高兴解决您的问题,现在我将为未来的访问者发布一个答案,包括两种方法。我使用的是StaggedGridLayoutManager,这两种方法都非常有效。
if(isLolipop()) v.setTranslationZ(50);
public static boolean isLolipop(){
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
}