我想要一个来自底部的 View 和 Animation 来填充半个屏幕。在那个视图中,我会有很多 ImageButton。我可以从底部到半屏对动画进行编程,但之后我的视图将填满整个屏幕。
我想要的是:
我拥有的:
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>
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
有谁知道我可以为我的视图做些什么只填满半屏?
使用BottomSheet Android 组件可以轻松实现这种实现。
有两种类型的BottomSheets,Persistent和Modal Bottom Sheet。
1. 持久底片
Persistent 底部工作表显示应用内内容。它将显示在屏幕底部,使部分内容可见。激活后,它会打开完整内容。持久底部工作表的高度与应用程序相同,使其成为应用程序的一部分。以下是 Google Maps 应用程序持久性底部工作表的示例。
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
}
});
Run Code Online (Sandbox Code Playgroud)
展开BottomSheet:
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
Run Code Online (Sandbox Code Playgroud)
折叠BottomSheet:
behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
Run Code Online (Sandbox Code Playgroud)
根据您的需要,您可以选择BottomSheet 之上的任何类型。希望这有助于解决您的问题。
| 归档时间: |
|
| 查看次数: |
2567 次 |
| 最近记录: |