效果图:
代码:
public class ViewActivity extends Activity implements View.OnClickListener {
Dialog mDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
initDialog(this);
}
@Override
public void onClick(View v) {
mDialog.show();
}
public void initDialog(Context context) {
View view = LayoutInflater.from(context).inflate(R.layout.view_share, null);
mDialog = new Dialog(context, R.style.Translucent_Dialog);
mDialog.setContentView(view);
ImageView iv_cancel = (ImageView) view.findViewById(R.id.iv_cancel);
iv_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDialog.dismiss();
}
});
}
}
透明背景的样式:
<style name="Translucent_Dialog" parent="@android:style/Theme.Dialog">
<!-- 设置自定义布局的背景透明 -->
<item name="android:background">@android:color/transparent</item>
<!-- 设置window背景透明,也就是去边框 -->
<item name="android:windowBackground">@android:color/transparent</item>
<!-- 设置Activity背景是否变灰 -->
<item name="android:backgroundDimEnabled">false</item>
</style>
更改backgroundDimEnabled可以去掉灰色背景,效果如图:
tip:
//想要有这种透明效果的对话框时,不用使用AlertDialog.Builder创建dialog,否则效果会异常
时间: 2024-10-21 05:56:58