【问题标题】:How to make a dialog slide from bottom to middle of screen in android如何在android中使对话框从屏幕底部滑到屏幕中间
【发布时间】:2012-02-16 04:30:32
【问题描述】:

我想用动画显示我的活动对话框。我的对话框将从活动底部滑到活动中间。

/****编辑****/

对不起,我的问题不清楚。我的意思是我的对话框将从底部滑到中间,但对话框的底部放置在活动的底部,如下图

【问题讨论】:

  • 看看github.com/Ali-Rezaei/SlidingDrawer,它可以通过几行代码从任何一侧滑动。
  • 我没有看到一个例子,你已经给出了一个布局 xml,但是如何在代码中使用它?你能举个例子吗?
  • 这对大多数人来说已经过时了,但对于新手来说仍然值得注意 - 不要在 Android 上复制 iOS 体验。根据 Google (material.io) 的建议,请参阅 Material Design 指南以了解应该做什么和不应该做什么。

标签: android


【解决方案1】:

为此,您需要 2 个动画并将其放在 res/anim 文件夹中

  1. slide_up_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromYDelta="50%p" android:toYDelta="0%p"
    android:duration="@android:integer/config_longAnimTime"/>

2.slide_out_down.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_longAnimTime"
    android:fromYDelta="0%p"
    android:toYDelta="100%p" />

现在您必须在 style.xml 中创建自定义样式

<style name="DialogAnimation">
        <item name="android:windowEnterAnimation">@anim/slide_up_dialog</item>
        <item name="android:windowExitAnimation">@anim/slide_out_down</item>
</style>

接下来是扩展android Theme。对话框主题在同一个style.xml中,并引用我们创建的自定义样式。

<!-- Animation for dialog box -->
    <style name="DialogSlideAnim" parent="@android:style/Theme.Dialog">
        <item name="android:windowAnimationStyle">@style/DialogAnimation</item>
    </style>

最后,在创建这样的对话框时调用此样式。

dialog = new Dialog(new ContextThemeWrapper(this, R.style.DialogSlideAnim));

是的……现在对话框可以滑动了……!!

更新:

正如@MichealP 建议的那样,这会将窗口放在底部

getWindow().setGravity(Gravity.BOTTOM); 

并修改样式以去除标题和背景

<item name="android:windowBackground">@null</item> 
<item name="android:windowFrame">@null</item> 
<item name="android:windowNoTitle">true</item>

正如@sikni8 建议的那样,这将使黑色边框透明

getWindow().setBackgroundDrawableResource(android.R.color.transparent);

【讨论】:

  • 我添加了 getWindow().setGravity(Gravity.BOTTOM);和 ​​@null@nulltrue 然后它按我的意愿工作
  • @MichaelP 对我来说并不是来自底部。它只是开始像 grow_from_middle 这样的动画,但在屏幕底部。有什么我错过的吗?
  • 你也可以使用getWindow().setBackgroundDrawableResource(android.R.color.transparent);让黑色边框在运行时透明。
  • 你好@arunsoorya,我实现了你的对话框动画解决方案,但后来我实现了“Android操作栏样式生成器”文件和它生成的主题,现在用于创建对话框的样式“hintDialog =新对话框(c,R.style.DialogSlideAnim);“正在显示具有透明背景的对话框。我应该修复什么?谢谢:)
  • 对我来说 dialog = new Dialog(new ContextThemeWrapper(this, R.style.DialogSlideAnim));没用..相反,我使用了这个- dialog.getWindow().setWindowAnimations(R.style.DialogAnimation);
【解决方案2】:

我在这里尝试了所有答案,但它对我不起作用。我知道所有答案都是很久以前写的。所以让我展示一下我是如何让它工作的。 I followed this article.

简而言之: 创建 res/anim/slide_up.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
      android:duration="@android:integer/config_mediumAnimTime" 
      android:fromYDelta="100%" 
      android:interpolator="@android:anim/accelerate_interpolator" 
      android:toYDelta="0">
    </translate>
</set>

然后,创建 res/anim/slide_bottom.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
        android:duration="@android:integer/config_mediumAnimTime" 
        android:fromYDelta="0%p" 
        android:interpolator="@android:anim/accelerate_interpolator" 
        android:toYDelta="100%p">
    </translate>
</set>

然后在 res/values/styles.xml 中添加一个样式

<style name="DialogAnimation">
    <item name="android:windowEnterAnimation">@anim/slide_up_dialog</item>
    <item name="android:windowExitAnimation">@anim/slide_out_down</item>
</style>

现在您可以将此动画样式设置为您的对话框或警报对话框,如下所示。

Dialog dialog = new Dialog(this);
dialog.getWindow().getAttributes().windowAnimations = animationSource;

或者,

Dialog dialog = new Dialog(this);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.BOTTOM;
lp.windowAnimations = R.style.DialogAnimation;
dialog.getWindow().setAttributes(lp);

我仅展示了对话框的示例,但正如我之前所说,您也可以将这种方法用于警告对话框。

【讨论】:

  • 是的,这是我从这里找到的完美答案。
【解决方案3】:

您可以使用模态底页 (reference)。

  1. 添加设计支持库

    implementation "com.android.support:design:$version_support"
    
  2. 创建一个扩展BottomSheetDialogFragmentFragment 并覆盖onCreateView

    class BottomDialogFragment : BottomSheetDialogFragment() { 
    
        companion object {
            fun newInstance() = BottomDialogFragment()
        }
    
        override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
            return inflater.inflate(R.layout.dialog_layout, container)
        }
    }
    
  3. 显示对话框

    val dialog = BottomDialogFragment.newInstance()
    dialog.show(supportFragmentManager, BottomDialogFragment::class.java.simpleName)
    

【讨论】:

  • 如何在BottomDIalogFragment中发送数据?
  • 与使用常规 Fragment 的方式相同,您创建一个 Bundle 并将数据放在那里
【解决方案4】:

您可以使用底部表格。我放了一些关于它的信息。

在 Android 支持库 23.2 中,Google 宣布支持底部表格。根据 Material Design,底部表格是仅作为用户启动操作的结果显示的元素,用于显示更多内容。

有两种主要类型的底片:

模态底页是菜单或简单对话框的替代品。他们还可以展示来自其他应用的深层链接内容。它们主要用于移动设备。

持久性底页呈现应用内内容

有一个简单的例子

在 Android 上制作 BottomSheet 非常简单,您只需使用 CoordinatorLayout 作为布局的主要元素,并将 BottomSheet 行为附加到视图。 p>

1 步 - activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<Button
    android:id="@+id/btnButtonSheet"
    android:text="Camera"
    android:textColor="#1e1e1e"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginBottom="8dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<!-- Adding bottom sheet after main content -->
<include layout="@layout/bottom_sheet" />

</android.support.design.widget.CoordinatorLayout>

2 步 - 添加 bottom_sheet.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="vertical"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="center_vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Select your options!"
        android:gravity="center"
        android:textColor="#1e1e1e"
        android:textSize="16dp"
        android:layout_margin="8dp"
        android:textStyle="bold" />

</LinearLayout>
<Button
    android:id="@+id/btnPhoto"
    android:text="Photo"
    android:textColor="#1e1e1e"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginBottom="8dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/btnCamera"
    android:text="Camera"
    android:textColor="#1e1e1e"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginBottom="8dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/btnCancel"
    android:text="Cancel"
    android:background="#a2a2a3"
    android:textColor="#1e1e1e"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginBottom="8dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

3 步 - 让你的 MainActivity 像这样:

public class MainActivity extends AppCompatActivity {

@BindView(R.id.btnButtonSheet)
Button btnBottomSheet;

@BindView(R.id.bottom_sheet)
LinearLayout layoutBottomSheet;

BottomSheetBehavior sheetBehavior;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    sheetBehavior = BottomSheetBehavior.from(layoutBottomSheet);


    sheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            switch (newState) {
                case BottomSheetBehavior.STATE_HIDDEN:
                    break;
                case BottomSheetBehavior.STATE_EXPANDED: {
                    btnBottomSheet.setText("Close");
                }
                break;
                case BottomSheetBehavior.STATE_COLLAPSED: {
                    btnBottomSheet.setText("Expand");
                }
                break;
                case BottomSheetBehavior.STATE_DRAGGING:
                    break;
                case BottomSheetBehavior.STATE_SETTLING:
                    break;
            }
        }

        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset) {

        }
    });
}

@OnClick(R.id.btnButtonSheet)
public void toggleBottomSheet() {
    if (sheetBehavior.getState() != BottomSheetBehavior.STATE_EXPANDED) {
        sheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        btnBottomSheet.setText("Close Bottom sheet");
    } else {
        sheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
        btnBottomSheet.setText("Expand Bottom sheet");
    }
}
}

【讨论】:

  • 对于新手 - 有一个用例可以说明使用底页不是的好主意 - 例如,当您不希望通过拉动关闭对话框时用你的手指向下。一个用例是您在每个项目上使用 ItemTouchHelper 的项目列表 - 在这种情况下,您需要从 BottomSheetDialog 覆盖这么多方法,使用已完成的解决方案变得毫无意义。
【解决方案5】:

这是在显示时为对话框设置动画的最简单方法

dialog.setOnShowListener(new DialogInterface.OnShowListener() {

    @Override
    public void onShow(DialogInterface dialogInterface) {
        View view = dialog.getWindow().getDecorView();
        //for enter from left
        //ObjectAnimator.ofFloat(view, "translationX", -view.getWidth(), 0.0f).start(); 

        //for enter from bottom
        ObjectAnimator.ofFloat(view, "translationY", view.getHeight(), 0.0f).start();
    }

});

除此之外,从底部动画时使对话框背景全屏透明

Window window = dialog.getWindow();
window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
window.setBackgroundDrawableResource(android.R.color.transparent);

【讨论】:

    【解决方案6】:

    除了 arunsoorya 的回答:

    更改 slide_up_dialog.xml

    <?xml version="1.0" encoding="utf-8"?>
    <translate xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="@android:integer/config_longAnimTime"
        android:fromYDelta="0%p"
        android:toYDelta="100%p" />
    

    还有slide_out_down.xml

    <?xml version="1.0" encoding="utf-8"?>
    <translate xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromYDelta="0%p" 
        android:toYDelta="50%p"
        android:duration="@android:integer/config_longAnimTime"/>
    

    【讨论】:

      【解决方案7】:

      新的 Material Design 库为您提供 BottomSheetDialog 以实现精确的外观和更轻松的实现

      【讨论】:

        【解决方案8】:

        除了所有其他答案之外,您还可以将此动画用于顶栏。 参考这里https://www.tutlane.com/tutorial/android/android-slide-up-down-animations-with-examples

        slide_up.xml

        <?xml version="1.0" encoding="utf-8"?>
        <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator">
            <scale
                android:duration="500"
                android:fromXScale="1.0"
                android:fromYScale="1.0"
                android:toXScale="1.0"
                android:toYScale="0.0" />
        </set>
        

        slide_down.xml

        <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator">
            <scale
                android:duration="500"
                android:fromXScale="1.0"
                android:fromYScale="0.0"
                android:toXScale="1.0"
                android:toYScale="1.0" />
        </set>
        

        输出如下所示

        【讨论】:

          猜你喜欢
          • 2016-01-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-11-18
          • 2020-03-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多