自定义dialog(仿照APP启动时的dialog广告页面)

*看朋友的博客的,觉得还不错,就学学,自定义弹窗用的是自定义dialog,没有用dialogFragment
http://blog.csdn.net/qq_34471736/article/details/70217403 朋友博客链接地址

自定义dialog(仿照APP启动时的dialog广告页面)_第1张图片
是一个从下面弹出的动画效果
自定义dialog(仿照APP启动时的dialog广告页面)_第2张图片
感觉现在app都有这种dialog了,于是学学呗。
项目的完整代码,点击我朋友博客链接,下边有,虽然还要一积分,哈哈
主要贴图二的代码,
自定义一个dialog集成dialog

package nan.dialogview;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.View;
import android.widget.ImageView;

/**
 * Created by NanFeiLong on 2017/4/16.
 */

public class MyCloseDialog extends Dialog implements View.OnClickListener{
    private ImageView img_back;
    private  Context mContext;

    public MyCloseDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.dialog_closeicon_layout);
        img_back=(ImageView) findViewById(R.id.close);
        img_back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.close:
                MyCloseDialog.this.dismiss();
                break;
            default:
                break;
        }

    }
}

下边贴布局


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:layout_centerInParent="true">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_margin="12dp"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/im" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:gravity="center"
                android:text="点击领取更多"
                android:textColor="@android:color/white"
                android:textSize="26sp"
                android:textStyle="bold" />
        LinearLayout>

        <ImageView
            android:id="@+id/close"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/close" />


    RelativeLayout>


LinearLayout>

这就是dialog 的全部代码,
下边贴下mainActivity

package nan.dialogview;

import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button button;
    private Button btnClose;
    private Dialog mDialog;
    private MyCloseDialog myCloseDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
        btnClose = (Button) findViewById(R.id.btn_close_dialog);
        btnClose.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

       switch (v.getId()){
           case R.id.button:
               mDialog = DialogUtil.ShowBabyShowSendDialog(MainActivity.this,this,this);
               mDialog.show();
               break;
           case R.id.iv_send_picture:
               Toast.makeText(this,"点击了图片",Toast.LENGTH_SHORT).show();
               mDialog.dismiss();
               break;
           case R.id.iv_send_vedio:
               Toast.makeText(this,"点击了视频",Toast.LENGTH_SHORT).show();
               mDialog.dismiss();
               break;
           case R.id.btn_close_dialog:
               //若想让弹出的dialog之外的背景为半透明,第二个参数传0 或者自定义样式
               myCloseDialog = new MyCloseDialog(this,0);
               myCloseDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//代码中取消标题栏
               myCloseDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
               myCloseDialog.show();
               myCloseDialog.setCancelable(false);
               break;
           default:
               break;
       }

    }
}

好了,就这么多,需要用的,直接cv走就好

你可能感兴趣的:(android)