DialogPlus原理

2016-07-17  本文已影响342人  Alien的小窝

DialogPlus 源码分析

简化后的代码如下

package com.ty.helloworld;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

public class MainActivity extends AppCompatActivity {

    private ViewGroup decorView;
    private ViewGroup rootView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



    }

    public void showdialog(){

//        DialogPlus.newDialog(this)
//                .setContentHolder(new ViewHolder(R.layout.item_dialog))
//                .setGravity(Gravity.BOTTOM)
//                .create().show();

        Animation  inAnim = AnimationUtils.loadAnimation(this, R.anim.slide_in_bottom);
        Animation outAnim= AnimationUtils.loadAnimation(this, R.anim.slide_out_bottom);


        LayoutInflater layoutInflater = LayoutInflater.from(this);
//        decorView = (ViewGroup) this.getWindow().getDecorView().findViewById(android.R.id.content);
        decorView = (ViewGroup) this.getWindow().getDecorView();
        rootView = (ViewGroup) layoutInflater.inflate(R.layout.rootview, decorView, false);
//        rootView.setLayoutParams(); //设置布局参数

        View outmostView = rootView.findViewById(R.id.dialogplus_outmost_container); //  第一个FrameLayout,设置背景颜色用的
        outmostView.setBackgroundResource(R.color.dialogplus_black_overlay); // 0x7f0b0015

         ViewGroup  contentContainer = (ViewGroup) rootView.findViewById(R.id.dialogplus_content_container); //第二个FrameLayout,存放内容用的
//        contentContainer.setLayoutParams(); 设置布局参数

//        初始化各种布局等参数
        initContentView(contentContainer);


//  show      onAttached(rootView);
        decorView.addView(rootView);
//  show      onAttached(rootView);

        contentContainer.startAnimation(inAnim);
        contentContainer.requestFocus();

////        dimiss
//        contentContainer.startAnimation(outAnim);
//        decorView.removeView(rootView);
//


    }

    private void initContentView(ViewGroup contentContainer) {
        View contentView = getLayoutInflater().inflate(R.layout.contentview, null);
//        contentView.setLayoutParams();
        contentContainer.addView(contentView);

    }


    public void show(View view) {
        showdialog();
    }

    @Override
    public void onBackPressed() {
        decorView.removeView(rootView);
    }
}


容器rootview.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    android:id="@+id/dialogplus_outmost_container"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/dialogplus_black_overlay"
    android:clickable="true"
    >

    <FrameLayout
        android:id="@+id/dialogplus_content_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        >
    </FrameLayout>

</FrameLayout>

内容布局contentview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#ff0"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_gravity="center"
    android:layout_height="wrap_content">

    <TextView
        android:gravity="center"
        android:text="这是内容啊"
        android:layout_width="300dp"
        android:layout_height="200dp"/>

</LinearLayout>

说明

显示Dialog原理是 onAttached(rootView); 即获取当前Activity的DectorView(FrameLayout或DectorView中的不带Toolbar的容器), 添加一个View就是显示Dialog,removeView就是取消Dialog

上一篇 下一篇

猜你喜欢

热点阅读