【问题标题】:Increase height of linear layout by animation通过动画增加线性布局的高度
【发布时间】:2015-07-05 13:39:59
【问题描述】:

我想在 2 秒后增加线性布局的高度。 加载活动后,应用程序应等待 2 秒,然后线性布局的高度应从 200dp 增加到 300dp。 xml代码如下所示。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fffafaf9"
android:clipChildren="false"

 tools:context=".MainActivity">

<TextView android:text="@string/hello_world"
    android:id="@+id/top"
    android:layout_width="wrap_content"
    android:layout_height="60dp"
    android:paddingTop="30dp"
    android:paddingLeft="20dp"
    />

<View
    android:layout_width="fill_parent"
    android:layout_height="2dp"
    android:layout_below="@id/top"
    android:background="#ffd3d3d2" />
<LinearLayout android:layout_width="fill_parent"
    android:layout_height="200dp"
    android:layout_alignParentBottom="true"
    android:orientation="vertical">

    <View
    android:id="@+id/temp_view"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:visibility="visible"
    android:translationZ="20dp"

    android:layout_gravity="center_horizontal"
    android:background="@drawable/circle_shape"
    />

<LinearLayout

    android:layout_width="fill_parent"
    android:layout_height="200dp"
    android:layout_marginTop="-80px"
    android:background="#ffe4e4e3"
    android:elevation="-10dp"
    android:alpha="0.5"
    android:layout_below="@id/temp_view"
    >

</LinearLayout>

【问题讨论】:

    标签: android animation android-linearlayout


    【解决方案1】:

    你可以像这样编写一个自定义的 LinearLayout:

    public class CustomLinearLayout extends LinearLayout {
    
    private int mLayoutHeight;
    
    public CustomLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public void setLayoutHeight(int height) {
        mLayoutHeight = height;
        ViewGroup.LayoutParams lp = getLayoutParams();
        lp.height = height;
        setLayoutParams(lp);
    }
    
    public int getLayoutHeight() {
        return mLayoutHeight;
    }
    

    }

    然后在你的activity中找到LinearLayout并启动动画:

     customLayout = (CustomLinearLayout) findViewById(R.id.cl);
        ObjectAnimator oa = ObjectAnimator.ofInt(customLayout, "layoutHeight", DensityUtil.dip2px(this, 200), DensityUtil.dip2px(this, 300));
        oa.setDuration(2000);
        oa.start();
    

    【讨论】:

    • 不,它不起作用。线性布局高度不变
    • 我运行了我编写的代码,它有效,请更新您的代码@PranavAgrawal
    猜你喜欢
    • 2017-11-23
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 2014-12-24
    • 2014-01-06
    • 1970-01-01
    相关资源
    最近更新 更多