删除高程后,CardView占用高程空间

时间:2016-07-17 13:56:22

标签: android android-layout android-cardview

当我降低CardView的高度时,需要升高空间
这是正常的照片: - enter image description here

点击时: - enter image description here

这是再次点击,我尝试将其高程恢复正常: - enter image description here

点击的项目是(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" 

点击卡片视图: -

@Override
                public void onClick(View arg0) {
                    if (i % 2 == 1) {
                        mAddIcon.setVisibility(View.VISIBLE);
                        Animation anim = AnimationUtils.loadAnimation(context,
                                R.drawable.settrans);
                        mAddIcon.startAnimation(anim);
                        mAddIcon.setImageResource(R.drawable.ic_done_black_24dp);
                        i++;
                        Categories cat = new Categories();
                        cat.mCategory = mCategory.getText().toString();
                        mSelectedList.add(cat);
                        Random rnd = new Random(); 
                        String[] color= {"#F44336","#FF4081","#9C27B0","#7C4DFF","#009688","#00BCD4","#8BC34A","#CDDC39","#FF5722","#607D8B"};
                        int index = rnd.nextInt(color.length-1);
                        String item = color[index];
                        //int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));   
                    //  card.setBackgroundColor(color);
                    //  card.setBackgroundColor(Color.parseColor(item));
                    //  mCategory.setTextColor(Color.parseColor("#ffffff"));
                        card.setElevation(50);
                    } else {
                        Animation anim = AnimationUtils.loadAnimation(context,
                                R.drawable.settrans);
                        mAddIcon.startAnimation(anim);
                        mAddIcon.setImageResource(R.drawable.ic_add_black_24dp);
                        i++;
                        Categories cat = new Categories();
                        cat.mCategory = mCategory.getText().toString();
                        mSelectedList.remove(cat);
                        mCategory.setTextColor(Color.parseColor("#000000"));
                    card.setBackgroundColor(Color.parseColor("#fffff7"));
                        card.setElevation(10);
                    }
                }
            });

1 个答案:

答案 0 :(得分:1)

设置View而不是CardView的提升或翻译Z对您有所帮助。

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;
}