具有透明背景的BottomSheetDialog [英] BottomSheetDialog with transparent background
问题描述
我想显示一个底表对话框,其宽度小于屏幕宽度.
I would like to display a bottom sheet dialog less wide than the screen width.
例如,Nexus 9上Google Play音乐中的共享"选项.
For instance, the Share option from Google Play Music on a Nexus 9.
您知道如何实现吗?
目前,我只是成功地减少了工作表内容的宽度,但是背景仍然处于屏幕宽度并显示白色背景.
For now I just succed to reduce the width of the sheet content but the background is still at the screen width and display a white background.
某些代码:
build.gradle
build.gradle
compile 'com.android.support:design:23.3.0'
MainActivity
MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
...
mBottomSheetDialog = new BottomSheetDialog(this);
mBottomSheetDialog.setContentView(R.layout.sheet_test);
mBottomSheetDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
mBottomSheetDialog = null;
}
});
mBottomSheetDialog.show();
}
sheet_test
sheet_test
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
style="@style/TextAppearance.AppCompat.Body1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="Some Text"
android:textColor="@color/colorPrimary" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ddd" />
<TextView
style="@style/TextAppearance.AppCompat.Body1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:text="Some Text" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ddd" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
推荐答案
因此,我想出了两种解决方案.
So, I figured out 2 solutions.
最佳之一:
为底页创建具有透明背景的活动. 通过协调器布局和底页来实现自己的布局. 设置所需的边距. 设置所需的内容.
Create an activity with transparent background just for your bottom sheet. Implement your own layout with a coordinator layout and a bottom sheet. Set the margin you want. Set the content you want.
尚未测试.
懒惰的人:
在onActivityCreated
中扩展 BottomSheetDialogFragment . :
Resources resources = getResources();
// Set margin for Landscape Mode. Maybe a more elegant solution will be to implements our own bottom sheet with our own margins.
if (resources.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
assert getView() != null;
View parent = (View) getView().getParent();
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) parent.getLayoutParams();
layoutParams.setMargins(
resources.getDimensionPixelSize(R.dimen.bottom_sheet_margin_left), // 64dp
0,
resources.getDimensionPixelSize(R.dimen.bottom_sheet_margin_right), // 64dp
0
);
parent.setLayoutParams(layoutParams);
}
这篇关于具有透明背景的BottomSheetDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!