【问题标题】:Expandable height of CardViewCardView 的可展开高度
【发布时间】:2017-07-10 14:52:15
【问题描述】:

我正在处理CardView 上的属性的展开和折叠。

public class SimpleCardView extends CardView {
private int animationDuration;


   public SimpleCardView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public SimpleCardView(Context context) {
        super(context);
    }

    public SimpleCardView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void expand(){
        final int initialHeight = getHeight();
        final float scale = getContext().getResources().getDisplayMetrics().density;
        int targetHeight = (int) (232 * scale + 0.5f);
        final int distance_to_expand = targetHeight - initialHeight;
        Animation animation = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                getLayoutParams().height = (int) (initialHeight +(distance_to_expand*interpolatedTime));
                requestLayout();
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };
        animationDuration = distance_to_expand;
        animation.setDuration((long)distance_to_expand);
        startAnimation(animation);
    }

    public int getAnimationTime(){
        return animationDuration;
    }

    public void collapse(){}

}

这是我的截图:

我正在设置目标高度的常数值。

int targetHeight = (int) (232 * scale + 0.5f);

这里,targetHeightCardView 的可展开高度。 因此,当内容过长时,只会显示一小部分内容。

有没有办法动态设置高度而不是一个恒定值?

【问题讨论】:

  • 简单的解决方案兄弟。设置 CardView 高度 wrap_content setYour Title 正如你所做的那样,TextView 下面的内容可见性应该消失了。所以当你点击展开时,试着看到包含内容的 TextView。
  • 让视图可见性可见/消失是个好主意。现在我不那样做。那么,有什么方法可以动态设定目标高度。

标签: android cardview


【解决方案1】:

你尝试过 :wrap_content

android:layout_height="wrap_content"

<android.support.v7.widget.CardView
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content">

你需要删除静态高度表单类SimpleCardView

**或者你需要计算身高**

为此,您需要将 Text 设置为 TextView,然后 使用下面的行计算TextView 的高度,然后通过任何设置方法传递到SimpleCardView

TextView textview ;

textveiw.setText("your text");
textview.mesure(0,0);
int height = textview.getMesuredHeight();

【讨论】:

  • 有没有办法将高度动态地放在SimpleCardView类中作为WRAP_CONTENT
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-30
  • 1970-01-01
  • 2019-01-25
  • 2022-01-22
  • 2018-06-21
  • 2022-10-13
相关资源
最近更新 更多