首页 > 解决方案 > 从底部到半屏查看

问题描述

我想要一个从底部带有动画的视图来填充半屏。在那个视图中,我会有很多 ImageButtons。我可以将动画从底部编程到半屏,但之后我的视图会填满整个屏幕。

我想要的是:

在此处输入图像描述

我有的:

在此处输入图像描述

xml代码:

<android.support.v4.widget.DrawerLayout
        android:id="@+id/screen_dashboard" style="@style/drawer"
        tools:openDrawer="start">
(...)
<TableLayout
            android:id="@+id/hidden_panel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:visibility="gone" >

            <TableRow
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <ImageButton
                    android:id="@+id/button1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/app_name"
                    android:onClick="slideUpDown" />
                <ImageButton
                    android:id="@+id/button2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/app_name"
                    android:onClick="slideUpDown" />
            </TableRow>
        </TableLayout>
    </android.support.v4.widget.DrawerLayout>

Java 代码:

public void slideUpDown(final View view) {

        if (!isPanelShown()) {
            // Show the panel
            Animation bottomUp = AnimationUtils.loadAnimation(this, R.animator.bottom_up);
            hiddenPanel.startAnimation(bottomUp);

            hiddenPanel.setVisibility(View.VISIBLE);
        }
        else {
            // Hide the Panel
            Animation bottomDown = AnimationUtils.loadAnimation(this, R.animator.bottom_down);

            hiddenPanel.startAnimation(bottomDown);
            hiddenPanel.setVisibility(View.GONE);
        }
    }

    private boolean isPanelShown() {
        return hiddenPanel.getVisibility() == View.VISIBLE;
    }

有谁知道我可以为我的视图做些什么来填满半屏?

标签: android

解决方案


使用BottomSheet Android 组件可以轻松实现这种实现。

BottomSheets有两种类型,PersistentModal Bottom Sheet。

1. 持久底片

Persistent 底部工作表显示应用内内容。它将显示在屏幕底部,使部分内容可见。激活后,它会打开全部内容。持久底页的高度与应用程序相同,使其成为应用程序的一部分。下面是谷歌地图应用的持久底页的例子。

在此处输入图像描述

2. 模态底板

模态底部工作表比应用程序具有更高的高度。这些通常替换菜单或对话框。通常用于显示来自其他应用程序的深层链接内容的模态底页。以下是 Google Drive 应用程序的模态底页示例。

在此处输入图像描述

BottomSheet 的示例代码片段:

View bottomSheet = findViewById(R.id.bottom_sheet);
        behavior = BottomSheetBehavior.from(bottomSheet);
        behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                // React to state change
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {
                // React to dragging events
            }
        });

要展开 BottomSheet:

behavior.setState(BottomSheetBehavior.STATE_EXPANDED);

要折叠 BottomSheet:

behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);

根据您的需要,您可以选择任何类型的 BottomSheet。希望这有助于解决您的问题。


推荐阅读