【问题标题】:Expand and collapse animation on LinearLayout with weights使用权重在 LinearLayout 上展开和折叠动画
【发布时间】:2015-01-13 06:12:54
【问题描述】:

您好,我设计了如下线性布局:

<Linear Layout 1 > -- > weightSum = 100
 <Linear Layout 2> -- > weight ->10
 <Linear Layout 3> -- > weight ->10
 <Linear Layout 4> -- > weight ->60
    <listview></listview>
 <Linear Layout 5 > -- > weight ->10
    <Linear Layout> ->visibility GONE
 <Linear Layout 6> -- > weight ->10
</Linear Layout > 

我使用了权重,以便布局高度固定且不可滚动。

现在我要创建&lt;Linear Layout&gt; whose visibility is GONE to VISIBLE onclick,在这种情况下,我可以降低 ListView 的高度,以便扩展的 LinearLayout 正确填充。

我使用了下面的动画,但它缩小了上面的视图或线性布局。

public static void expand(final View v) {
    v.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    final int targetHeight = v.getMeasuredHeight();

    v.getLayoutParams().height = 0;
    v.setVisibility(View.VISIBLE);
    Animation a = new Animation()
    {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            v.getLayoutParams().height = interpolatedTime == 1
                    ? LayoutParams.WRAP_CONTENT
                    : (int)(targetHeight * interpolatedTime);
            v.requestLayout();
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    // 1dp/ms
    a.setDuration((int)(targetHeight / v.getContext().getResources().getDisplayMetrics().density));
    v.startAnimation(a);
}

【问题讨论】:

  • 快速浏览您的代码,所以我可能错了。但是权重总和是 100,所有可见 ViewGroups 的总和也是 100。在动画中,您没有使用权重,新可见的 LinearLayout 的权重保持为 0。
  • @VarunSingh 是的,你是对的,我需要使用除权重以外的其他东西,以便将其高度固定到不同的屏幕布局
  • 根据Android's guide,您应该避免使用LinearLayout 并尽可能使用RelativeLayout 或GridLayout。此外,如果 minSDK >= 11,请改用 ObjectAnimator。它更易于理解和使用。
  • @VarunSingh 我们可以使用相对布局将布局高度固定到所有屏幕吗?就像我们使用权重一样?
  • 不,我想不出一个明显的方法来实现这一点。但是看看GridLayout。您可以使用属性 rowSpan 指定权重属性。更多关于 GridLayouthere

标签: android android-layout animation android-linearlayout


【解决方案1】:

试试这个

v.measure(0, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

【讨论】:

  • 不,还是一样,它缩小了上面的视图
猜你喜欢
  • 1970-01-01
  • 2011-09-12
  • 1970-01-01
  • 1970-01-01
  • 2017-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-21
相关资源
最近更新 更多