java CardView无法正确展开
我有一个TimerView类,它扩展了CardView。它最初折叠为仅显示第一行,并在点击时展开。问题是它没有像应该的那样扩展。在屏幕截图中,您可以看到底部图像比其他图像小,但三幅图像的分辨率都相同
下面是使其扩展的代码。单击的变量是静态的,我只使用它测量一次卡的高度,因为所有卡都是相同的
@Override
protected void onFinishInflate(){
super.onFinishInflate();
RelativeLayout upperCard = findViewById(R.id.upper_card);
upperCard.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
LinearLayout subCard = findViewById(R.id.sub_card);
if(!clicked){
foldedHeight = getMeasuredHeightAndState();
subCard.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
targetHeight = foldedHeight + subCard.getMeasuredHeightAndState();
clicked = true;
}
if(subCard.getVisibility() == View.GONE) {
subCard.setVisibility(View.VISIBLE);
ImageView arrow = findViewById(R.id.arrow);
arrow.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_keyboard_arrow_up_black_24dp));
ValueAnimator valueAnimator = ValueAnimator.ofInt(foldedHeight, targetHeight);
valueAnimator.setDuration(200);
valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
getLayoutParams().height = value.intValue();
requestLayout();
}
});
valueAnimator.start();
}
...
这是与TimerView相关的xml文件
<com.whatever.marco272.timepassedsince.TimerView
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:card_view="http://schemas.安卓.com/apk/res-auto"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
card_view:cardCornerRadius="5dp"
card_view:contentPadding="7dp"
card_view:cardElevation="3dp"
card_view:cardUseCompatPadding="true"
安卓:clickable="true"
安卓:foreground="?安卓:attr/selectableItemBackground">
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:orientation="vertical"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content" >
<RelativeLayout
安卓:id="@+id/upper_card"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content">
<TextView
安卓:id="@+id/title"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_gravity="left"
安卓:layout_marginRight="24dp"
安卓:textSize="24sp"
安卓:textStyle="normal|bold"
安卓:textColor="@安卓:color/holo_blue_dark"
安卓:text="Title" />
<TextView
安卓:id="@+id/days"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_toRightOf="@id/title"
安卓:layout_gravity="bottom"
安卓:textSize="24sp"
安卓:textStyle="normal|bold"
安卓:textColor="@安卓:color/black"
安卓:text="0" />
<ImageView
安卓:id="@+id/arrow"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignParentRight="true"
安卓:src="@drawable/ic_keyboard_arrow_down_black_24dp"/>
</RelativeLayout>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:id="@+id/sub_card"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:orientation="vertical"
安卓:visibility="gone">
<TextView
安卓:id="@+id/clock"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginBottom="10dp"
安卓:textSize="20sp"
安卓:textColor="@安卓:color/black"
安卓:text="0" />
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:id="@+id/add_button"
安卓:layout_height="wrap_content"
安卓:layout_width="wrap_content"
安卓:layout_marginBottom="10dp"
安卓:gravity="center_vertical">
<ImageView
安卓:src="@drawable/ic_add_button"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginRight="10dp"
/>
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textSize="18sp"
安卓:text="Update"/>
</LinearLayout>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:id="@+id/edit_button"
安卓:layout_height="wrap_content"
安卓:layout_width="wrap_content"
安卓:layout_marginBottom="10dp"
安卓:gravity="center_vertical">
<ImageView
安卓:src="@drawable/ic_edit_button"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginRight="10dp" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textSize="18sp"
安卓:text="Edit"/>
</LinearLayout>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:id="@+id/delete_button"
安卓:layout_height="wrap_content"
安卓:layout_width="wrap_content"
安卓:gravity="center_vertical">
<ImageView
安卓:src="@drawable/ic_delete_button"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginRight="10dp" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textSize="18sp"
安卓:text="Delete"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
# 1 楼答案
我怀疑这与
CardView
的填充有关。在棒棒糖前的API级别上,CardView
覆盖您为插入卡片而设置的任何填充(以便它可以在视图的边界内绘制“阴影”)。既然你已经设置了card_view:cardUseCompatPadding="true"
属性,这也将发生在棒棒糖上面我相信这种填充物可以解释你膨胀时“缺失”的高度