Android 弹窗动画 从下往上
时间: 2023-08-05 13:30:10 浏览: 103
您可以使用Android中的`BottomSheetDialog`来实现从下往上弹出的动画效果。您可以按照以下步骤操作:
1. 首先,在您的布局文件中定义一个`CoordinatorLayout`,并将其作为根布局。
2. 接着,在您的代码中创建一个`BottomSheetDialog`对象,并设置其`setContentView()`方法为您想要展示的布局。
3. 最后,在您的`BottomSheetDialog`对象中设置`Behavior`为`BottomSheetBehavior`,并调用`setPeekHeight()`方法来设置弹窗的高度。
以下是示例代码:
```java
// 定义 CoordinatorLayout 作为根布局
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.coordinatorlayout.widget.CoordinatorLayout>
// 创建 BottomSheetDialog 对象并设置 ContentView
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(this);
bottomSheetDialog.setContentView(R.layout.your_layout);
// 设置 Behavior 为 BottomSheetBehavior,并设置 peek height
View bottomSheet = bottomSheetDialog.findViewById(R.id.bottom_sheet);
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
bottomSheetBehavior.setPeekHeight(500);
```
在您的布局文件中,您需要定义一个`FrameLayout`,并将其作为`BottomSheetDialog`的根布局。您可以在该布局中添加您需要展示的内容。
```xml
<FrameLayout
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
app:behavior_hideable="true"
app:layout_behavior="@string/bottom_sheet_behavior">
// 添加您需要展示的内容
</FrameLayout>
```
注意,您还需要在您的`build.gradle`文件中添加以下依赖项:
```groovy
implementation 'com.google.android.material:material:1.2.1'
```
阅读全文
相关推荐
















