快速弹出窗口(三):自定义dialog实现广告弹窗

源码下载快速弹出窗口(三):自定义dialog实现广告弹窗_第1张图片

一.自定义dialog的使用

1.定义一个类继承dialog
2.修改style
3.加载布局
4.指定dialog在屏幕中的位置
 
    
  1. /自定义一个类继承dialog
  2. public class AdDialog extends Dialog implements View.OnClickListener {
  3. private Context context;
  4. public AdDialog(Context context) {
  5. // 更改样式,把背景设置为透明的
  6. super(context, R.style.LocatonDialogStyle);
  7. this.context = context;
  8. }
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. requestWindowFeature(Window.FEATURE_NO_TITLE);
  13. //加载dialog的布局
  14. setContentView(R.layout.view_location_dialog_dong);
  15. //拿到布局控件进行处理
  16. ImageView imageView = (ImageView) findViewById(R.id.mm);
  17. imageView.setOnClickListener(this);
  18. //初始化布局的位置
  19. initLayoutParams();
  20. }
  21. // 初始化布局的参数
  22. private void initLayoutParams() {
  23. // 布局的参数
  24. LayoutParams params = getWindow().getAttributes();
  25. params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER;
  26. params.alpha = 1f;
  27. getWindow().setAttributes(params);
  28. }
  29. @Override
  30. public void onClick(View v) {
  31. dismiss();
  32. }
  33. }
2.主页面代码
 
     
  1. public class MainActivity extends AppCompatActivity {
  2. private RocketView mRocketView;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. AdDialog dialog = new AdDialog(this);
  8. dialog.show();
  9. }
  10. }
3.布局和样式
 
     
  1. <style name="LocatonDialogStyle" parent="@android:style/Theme.Dialog">
  2. <item name="android:windowBackground">@android:color/transparentitem>
  3. <item name="android:windowAnimationStyle">@android:style/Theme.InputMethoditem>
  4. style>
 
      
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical" >
  5. <ImageView
  6. android:id="@+id/mm"
  7. android:src="@drawable/donghomesuccess"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"/>
  10. RelativeLayout>

你可能感兴趣的:(Ui,自定义控件)