以编程方式移动布局 - android
Shift layout programmatically - android
我正在实施一种方法,其中有片段,它包含带有一些数据的初始布局。现在,如果有通知弹出,我想将初始布局向下移动,如果通知消失,则向上移动。我想知道我该如何实施?如何根据通知是否可用上下移动布局?
这是一个设计。
layout
Image-A是没有通知的初始布局,Image-B是在顶部添加布局(通知)后的布局。
谢谢
只需在布局中始终显示通知,但在不应显示时将其可见性设置为“消失”。这告诉布局不显示它并且不占用任何 space 。然后当你想显示它时,将它设置为 VISIBLE。
您要实现的是横幅,您可以使用 Motion 布局来实现它,material components
上有关于它们的更多详细信息
按照这个tutorial,你可以很容易地实现它。
您可以在根布局上使用 animateLayoutChanges 为 true(考虑到通知和正文布局在同一个文件中)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:orientation="vertical">
<LinearLayout
android:id="@+id/notificationLayout"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="12dp"
android:background="@color/orange_300"
android:orientation="vertical">
<!-- body here -->
</LinearLayout>
<LinearLayout
android:id="@+id/mainBody"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="12dp"
android:background="@color/blue_300"
android:orientation="vertical" />
</LinearLayout>
当您更改 notificationLayout 可见性(VISIBLE 或 GONE)时,您将获得所需的动画。
我正在实施一种方法,其中有片段,它包含带有一些数据的初始布局。现在,如果有通知弹出,我想将初始布局向下移动,如果通知消失,则向上移动。我想知道我该如何实施?如何根据通知是否可用上下移动布局?
这是一个设计。 layout
Image-A是没有通知的初始布局,Image-B是在顶部添加布局(通知)后的布局。
谢谢
只需在布局中始终显示通知,但在不应显示时将其可见性设置为“消失”。这告诉布局不显示它并且不占用任何 space 。然后当你想显示它时,将它设置为 VISIBLE。
您要实现的是横幅,您可以使用 Motion 布局来实现它,material components
上有关于它们的更多详细信息按照这个tutorial,你可以很容易地实现它。
您可以在根布局上使用 animateLayoutChanges 为 true(考虑到通知和正文布局在同一个文件中)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:orientation="vertical">
<LinearLayout
android:id="@+id/notificationLayout"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="12dp"
android:background="@color/orange_300"
android:orientation="vertical">
<!-- body here -->
</LinearLayout>
<LinearLayout
android:id="@+id/mainBody"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="12dp"
android:background="@color/blue_300"
android:orientation="vertical" />
</LinearLayout>
当您更改 notificationLayout 可见性(VISIBLE 或 GONE)时,您将获得所需的动画。