【发布时间】:2018-06-08 20:03:40
【问题描述】:
我一直在尝试创建一个自定义的 AlertDialog,我知道我可以使用AlertDialog.Builder.setView() 在对话框中放置自定义视图,但是是否可以完全替换默认布局?
编辑:
我想这样做是因为它允许我使用构建器的setMessage()、setTitle() 等自定义布局
【问题讨论】:
-
您可以随时退回到Dialog,然后您可以控制一切。
我一直在尝试创建一个自定义的 AlertDialog,我知道我可以使用AlertDialog.Builder.setView() 在对话框中放置自定义视图,但是是否可以完全替换默认布局?
编辑:
我想这样做是因为它允许我使用构建器的setMessage()、setTitle() 等自定义布局
【问题讨论】:
可以自定义或完全更改Dialog或AlertDialog,您可以像这样自定义Dialog
private void customDialog() {
final Dialog dialog = new Dialog(ActivityUserVideoPlay.this, R.style.MaterialDialogSheet);
dialog.setContentView(R.layout.your_layout_foor_dialog); // your custom view.
dialog.setCancelable(false);
dialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
dialog.show();
}
这是对话框的样式
<style name="MaterialDialogSheet" parent="@android:style/Theme.Dialog">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowAnimationStyle">@style/MaterialDialogSheetAnimation</item>
</style>
根据需要使用动画打开或关闭对话框,否则可以将其删除。
希望对你有帮助。
【讨论】:
是的,您可以,您只需创建.xml 并对其进行膨胀。就这样:
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Get the layout inflater
LayoutInflater inflater = this.getLayoutInflater();
View alertView = inflater.inflate(R.layout.newsletter_dialog, null);
// Pass null as the parent view because its going in the dialog layout
builder.setView(alertView);
final AlertDialog dialog = builder.show();
【讨论】: