1. 程式人生 > >Android 實用Dialog

Android 實用Dialog

 <!--活動彈框-->
    <style name="MyDialog_black" parent="android:style/Theme.Dialog">
        <!--背景顏色及和透明程度-->
        <item name="android:windowBackground">@android:color/transparent</item>
        <!--是否去除標題 -->
        <item name="android:windowNoTitle">true</item>
        <!--是否去除邊框-->
        <item name="android:windowFrame">@null</item>
        <!--是否浮現在activity之上-->
        <item name="android:windowIsFloating">true</item>
        <!--是否背景黑-->
        <item name="android:backgroundDimEnabled">true</item>
    </style>
public class GuanggaoDialog extends Dialog {

    private Context context;

    private int layoutResID;

    private IndexHuodongImage indexHuodongImage;


    public GuanggaoDialog(Context context, int layoutResID, IndexHuodongImage indexHuodongImage) {
        super(context, R.style.MyDialog_black);
        this.context = context;
        this.layoutResID = layoutResID;
        this.indexHuodongImage = indexHuodongImage;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.setGravity(Gravity.CENTER); // 此處可以設定dialog顯示的位置為居中
//        window.setWindowAnimations(R.style.bottom_menu_animation); // 新增動畫效果
        window.setWindowAnimations(R.style.Animation_AppCompat_Dialog); // 新增動畫效果
        setContentView(layoutResID);
    
        // 點選Dialog外部消失
        setCanceledOnTouchOutside(true);

        //設定背景不透明
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.alpha=1f;//引數為0到1之間。0表示完全透明,1就是不透明。按需求調整引數

        ImageView imageView = findViewById(R.id.image_center);

        int screenWidth = ScreenUtils.getScreenWidth();
        int width = screenWidth - 20;


        ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
        layoutParams.width = width;
        layoutParams.height = width * 4 / 3;
        imageView.setLayoutParams(layoutParams);

        View view = findViewById(R.id.center_guanggao_root);
        Glide.with(context)
                .load(indexHuodongImage.getData().getActivity_pic())
                .apply(bitmapTransform(new RoundedCornersTransformation(8, 8)))
                .into(imageView);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, HuodongActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("imagegif", indexHuodongImage.getData().getContent_pic());
                intent.putExtras(bundle);
                context.startActivity(intent);
                dismiss();
            }
        });
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }


}