显示自定义布局警告对话框
我开发一个应用程序中,我必须表明它在屏幕截图中显示在屏幕上的按钮唯一的按钮。但是我想在其他一些活动上显示这个按钮。 例如我有一个在屏幕上的活动。现在两分钟后,我想显示一个自定义提示对话框,即屏幕截图中显示的这个屏幕,但不是只显示按钮,而是提示对话框将整个屏幕显示为一个对话框,即白色背景。我如何摆脱这种背景,只有这个按钮可以作为一个警告对话框显示在屏幕上? 我这个自定义布局代码:警报对话框
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="Unable to Snooze"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:id="@+id/button"
android:background="#088a68"
android:textColor="#FFFFFF"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp" />
和活动代码:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(ActivityA.this);
LayoutInflater inflater = ActivityA.this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alert, null);
dialogBuilder.setView(dialogView);
AlertDialog alert11 = dialogBuilder.create();
alert11.show();
}
}, 120000);
注意我要显示在屏幕底部的按钮作为alertdialog
编辑:
使用对话框代替AlertDialog.Builder,因此使用setContentView代替setView。并检查这一个:Dialog with transparent background in Android
老答案:
如果你是要告诉在多个活动这个按钮,你最好使用DialogFragment。此外,您可以将片段背景设置为透明,并且可以调整对话框的大小以适合按钮。
尝试用:
AlertDialog alert11 = dialogBuilder.create();
// Add this
alert11.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
alert11.show();
好吧,先生让我试试这个 –
不,它不工作:( –
试试这个: -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn_snooze"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Unable to snooze"/>
</LinearLayout>
&调用此方法可在任何需要
public void showDialog() {
dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setContentView(R.layout.dialog_layout);
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(true);
btn_snooze = (Button) dialog.findViewById(R.id.btn_snooze);
btn_snooze.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.cancel();
}
});
dialog.show();
}
问题已解决,但你的答案upvote :) –
小号我只想在一项活动中展示它。你可以编辑你的答案吗? –
请检查编辑答案。 – dcanbatman
好吧,我要去试试看,谢谢 –