gpt4 book ai didi

android - 动画改变 LayoutParams (RelativeLayout)

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:42:58 25 4
gpt4 key购买 nike

当我单击 RelativeLayout 时,它会更改大小。我怎样才能使这个变化动画化?所以它看起来像矩形增长到全屏?

我的布局:

<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#ff000000">

<RelativeLayout
android:id="@+id/srLayout"
android:layout_width="62.5dp"
android:layout_height="132.5dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="false"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:layout_marginTop="10dp"
android:clickable="true"
android:onClick="sleepRoom"
android:background="#ffffffff"></RelativeLayout>

</RelativeLayout>

代码:

public void sleepRoom(View view) {
RelativeLayout sr = (RelativeLayout) findViewById(view.getId());
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) sr.getLayoutParams();
params.height = RelativeLayout.LayoutParams.MATCH_PARENT;
params.width = RelativeLayout.LayoutParams.MATCH_PARENT;
sr.setLayoutParams(params);
}

最佳答案

对我有用的简单技巧:

添加:

android:animateLayoutChanges="true"

到您正在更改其 layoutParams 的 View 的 xml。

然后,在更改参数之前将这行代码添加到您的 Java/Kotlin 代码中:

Java:

((ViewGroup) sr).getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);

Kotlin :

(sr as ViewGroup).layoutTransition.enableTransitionType(LayoutTransition.CHANGING)

关于android - 动画改变 LayoutParams (RelativeLayout),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29900267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com