如何解决如何制作具有底部重力和 alpha 背景的警报对话框?
我正在使用警报对话框在 android 应用程序中创建一个对话框。我有一个问题。它的背景是全白的。我想要带有 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());
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。