android - 从底部到半屏查看
问题描述
我想要一个从底部带有动画的视图来填充半屏。在那个视图中,我会有很多 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;
}
有谁知道我可以为我的视图做些什么来填满半屏?
解决方案
使用BottomSheet Android 组件可以轻松实现这种实现。
BottomSheets有两种类型,Persistent和Modal 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。希望这有助于解决您的问题。
推荐阅读
- angular - 在 Angular 8 中向 http 调用添加轮询机制
- rust - 为什么选择
在匹配模式中失去可变性? - maven - 如何使用 Ansible 从 nexus 下载特定版本的 war 文件
- google-sheets - 将 IMPORTDATA() 与来自谷歌存储桶的对象一起使用
- c++ - 对 OpenMP 缩减子句中的变量进行原子操作
- javascript - 使用 javascript 为现有表单字段添加值
- google-chrome - WebRTC:检测 P2P 呼叫中远程对等点的流类型(视频/屏幕)
- python - 通过巧妙的导入管理 Python 模块依赖项
- python - Python:在不更改 csv 文件的情况下将列添加到加载的 csv 文件
- python-3.x - 如何计算多维列表中的重复项