无法获得全宽对话框
Can't get dialog with full width
我在我的应用程序中使用了底部滑动面板,为此我使用了自定义对话框,但是在对话框的两侧和 dialog.How 的底部有一些 space 我可以删除那个 space?
这是我的代码。
自定义布局
<LinearLayout
android:id="@+id/bottom_sheet"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:background="@color/colorPrimary"
android:paddingTop="30dp"
android:paddingBottom="30dp">
//elements of the layout
</LinearLayout>
自定义对话框JAVA
public class BottomPanel {
Context context;
public BottomPanel(Context context) {
this.context = context;
}
public Dialog showDialog() {
final Dialog dialog = new Dialog(context, R.style.CustomDialog);
WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
layoutParams.gravity = Gravity.BOTTOM;
dialog.setContentView(R.layout.bottom_sheet);
dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT);
dialog.getWindow().setAttributes(layoutParams);
dialog.setCancelable(true);
return dialog;
}
}
根据我的研究,对话框的默认 window 背景是 abc_dialog_material_background.xml
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="16dp"
android:insetTop="16dp"
android:insetRight="16dp"
android:insetBottom="16dp">
<shape android:shape="rectangle">
<corners android:radius="@dimen/abc_dialog_corner_radius_material" />
<solid android:color="@android:color/white" />
</shape>
</inset>
这就是为什么在显示对话框时您会看到每个站点的填充。有几种方法可以从对话框中删除所有填充。
最简单的方法是为对话框的 window 背景设置颜色背景。
<style name="CustomDialog" parent="Theme.AppCompat.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
</style>
另一种方法是为名为 background_dialog.xml
的对话框创建自定义背景
<inset xmlns:android="http://schemas.android.com/apk/res/android">
<shape android:shape="rectangle">
<corners android:radius="@dimen/abc_dialog_corner_radius_material" />
<solid android:color="@android:color/white" />
</shape>
</inset>
然后在 style.xml 文件中将其设置为对话框的 window 背景。
<style name="CustomDialog" parent="Theme.AppCompat.Dialog">
<item name="android:windowBackground">@drawable/background_dialog</item>
</style>
我在我的应用程序中使用了底部滑动面板,为此我使用了自定义对话框,但是在对话框的两侧和 dialog.How 的底部有一些 space 我可以删除那个 space?
这是我的代码。
自定义布局
<LinearLayout
android:id="@+id/bottom_sheet"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:background="@color/colorPrimary"
android:paddingTop="30dp"
android:paddingBottom="30dp">
//elements of the layout
</LinearLayout>
自定义对话框JAVA
public class BottomPanel {
Context context;
public BottomPanel(Context context) {
this.context = context;
}
public Dialog showDialog() {
final Dialog dialog = new Dialog(context, R.style.CustomDialog);
WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
layoutParams.gravity = Gravity.BOTTOM;
dialog.setContentView(R.layout.bottom_sheet);
dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT);
dialog.getWindow().setAttributes(layoutParams);
dialog.setCancelable(true);
return dialog;
}
}
根据我的研究,对话框的默认 window 背景是 abc_dialog_material_background.xml
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="16dp"
android:insetTop="16dp"
android:insetRight="16dp"
android:insetBottom="16dp">
<shape android:shape="rectangle">
<corners android:radius="@dimen/abc_dialog_corner_radius_material" />
<solid android:color="@android:color/white" />
</shape>
</inset>
这就是为什么在显示对话框时您会看到每个站点的填充。有几种方法可以从对话框中删除所有填充。
最简单的方法是为对话框的 window 背景设置颜色背景。
<style name="CustomDialog" parent="Theme.AppCompat.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
</style>
另一种方法是为名为 background_dialog.xml
<inset xmlns:android="http://schemas.android.com/apk/res/android">
<shape android:shape="rectangle">
<corners android:radius="@dimen/abc_dialog_corner_radius_material" />
<solid android:color="@android:color/white" />
</shape>
</inset>
然后在 style.xml 文件中将其设置为对话框的 window 背景。
<style name="CustomDialog" parent="Theme.AppCompat.Dialog">
<item name="android:windowBackground">@drawable/background_dialog</item>
</style>