[英]How to Transparent in Custom Alert Dialog in android
我为项目创建了自定义警报对话框。 当我单击按钮时,将打开“自定义警报”对话框,但在我的“自定义警报”对话框上会出现白色背景。 如何使其透明?
.java:
package com.example.bskes.customalertdialog;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button adbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adbtn = (Button) findViewById(R.id.btnCustomAD);
adbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
view = LayoutInflater.from(MainActivity.this).inflate(R.layout.rtr_mem_profile_layout, null);
builder.setPositiveButton("Thank You", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "It's My Pleasure", Toast.LENGTH_SHORT).show();
}
});
builder.setView(view);
builder.show();
}
});
}
}
您应该使用它,这将有助于显示透明的dilog背景
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
第一行显示背景上的透明层,第二行提供窗口视图的实际大小。 它帮助我,使用它
定义styles.xml
文件中的内容
<style name="CustomDialog" parent="android:Theme.Dialog">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
并将其作为参数传递给AlertDialog构造函数
AlertDialog.Builder imageDialog = new AlertDialog.Builder(SubProducts.this, R.style.CustomDialog);
或以编程方式,通过Dialog实例,您可以调用
myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT))
将此添加到您的styles.xml文件中
<style name="MyDialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowTitleStyle">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:background">@android:color/transparent</item>
</style>
将此添加到您的java文件:
Dialog dialog = new Dialog(this, R.style.MyDialog);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.