如何制作具有底部重力和 alpha 背景的警报对话框?
How to make a alert dialog with bottom gravity and alpha background?
我正在使用警报对话框在 android 应用程序中创建一个对话框。我有一个问题。它的背景是全白的。我想要对话框 window 的背景具有 alpha(半透明),如图所示。请帮我。
View layoutView = activity.getLayoutInflater().inflate(R.layout.alert_dialog_image, null);
AlertDialog.Builder builder = new AlertDialog.Builder(activity, R.style.DialogTheme);
builder.setView(layoutView);
final AlertDialog alertDialog = builder.create();
alertDialog.show();
您可以通过两种方式实现这一目标。
- BottomSheetDialog https://developer.android.com/reference/com/google/android/material/bottomsheet/BottomSheetDialog
- BottomSheetDialogFragment
https://developer.android.com/reference/com/google/android/material/bottomsheet/BottomSheetDialogFragment
例如fragment_bottom_sheet_dialog.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/lorem_ipsum"/>
</LinearLayout>
BottomSheetFragment.java
public class BottomSheetFragment extends BottomSheetDialogFragment {
public BottomSheetFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_bottom_sheet_dialog, container, false);
}
}
现在在activity/fragment里面,可以根据点击事件等任何事件调用这两个方法
public void showBottomSheetDialog() {
View view = getLayoutInflater().inflate(R.layout.fragment_bottom_sheet_dialog, null);
BottomSheetDialog dialog = new BottomSheetDialog(this);
dialog.setContentView(view);
dialog.show();
}
/**
* showing bottom sheet dialog fragment
* same layout is used in both dialog and dialog fragment
*/
public void showBottomSheetDialogFragment() {
BottomSheetFragment bottomSheetFragment = new BottomSheetFragment();
bottomSheetFragment.show(getSupportFragmentManager(), bottomSheetFragment.getTag());
}
我正在使用警报对话框在 android 应用程序中创建一个对话框。我有一个问题。它的背景是全白的。我想要对话框 window 的背景具有 alpha(半透明),如图所示。请帮我。
View layoutView = activity.getLayoutInflater().inflate(R.layout.alert_dialog_image, null);
AlertDialog.Builder builder = new AlertDialog.Builder(activity, R.style.DialogTheme);
builder.setView(layoutView);
final AlertDialog alertDialog = builder.create();
alertDialog.show();
您可以通过两种方式实现这一目标。
- BottomSheetDialog https://developer.android.com/reference/com/google/android/material/bottomsheet/BottomSheetDialog
- BottomSheetDialogFragment https://developer.android.com/reference/com/google/android/material/bottomsheet/BottomSheetDialogFragment
例如fragment_bottom_sheet_dialog.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/lorem_ipsum"/>
</LinearLayout>
BottomSheetFragment.java
public class BottomSheetFragment extends BottomSheetDialogFragment {
public BottomSheetFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_bottom_sheet_dialog, container, false);
}
}
现在在activity/fragment里面,可以根据点击事件等任何事件调用这两个方法
public void showBottomSheetDialog() {
View view = getLayoutInflater().inflate(R.layout.fragment_bottom_sheet_dialog, null);
BottomSheetDialog dialog = new BottomSheetDialog(this);
dialog.setContentView(view);
dialog.show();
}
/**
* showing bottom sheet dialog fragment
* same layout is used in both dialog and dialog fragment
*/
public void showBottomSheetDialogFragment() {
BottomSheetFragment bottomSheetFragment = new BottomSheetFragment();
bottomSheetFragment.show(getSupportFragmentManager(), bottomSheetFragment.getTag());
}