您的位置:首页 > 其它

自定义Dialog以及Dialog之间跳转

2015-08-26 15:12 423 查看
需求:注册分两步完成,每一步都是一个dialog

关键点:

自定义dialog

dialog跳转样式

ps:这里实现可能不是最佳方案,因为我并没有去根据屏幕大小去设置dialog的大小

效果





自定义dialog

[code]/**
 * Created by ${wj} ,
 * on 2015/8/11 0011.
 */
public class PhoneRegisterDialog extends Dialog{

    private Context mContext;

    public PhoneRegisterDialog(Context context, int theme) {
        super(context, theme);
        this.mContext=context;
    }

    public PhoneRegisterDialog(Context context) {
        this(context, R.style.myDialog);
        this.mContext=context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_phone_register);
        //点击dialog外无法关闭dialog
        setCancelable(false);

        ButterKnife.bind(this);
        this.getWindow().setWindowAnimations(R.style.dialog_anim);

    }

    @OnClick(R.id.next) void next(){
        this.dismiss();
        RegisterDialog registerDialog=new RegisterDialog(getContext());
        registerDialog.show();
    }


里面用到两个知识,一个是设置dialog的主题样式,一般都是如下设置

[code] <style name="myDialog" parent="Theme.AppCompat.Light.Dialog">
        <!-- 背景颜色及透明程度 -->
        <item name="android:windowBackground">@android:color/transparent</item>
        <!-- 是否有标题 -->
        <item name="android:windowNoTitle">true</item>
        <!-- 是否浮现在activity之上 -->
        <item name="android:windowIsFloating">true</item>
        <!-- 是否模糊 -->
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowFrame">@null</item>
    </style>


另外一个是dialog跳转的动画this.getWindow().setWindowAnimations(R.style.dialog_anim);

[code]<style name="dialog_anim">
        <item name="android:windowEnterAnimation">@anim/right_in</item>
        <item name="android:windowExitAnimation">@anim/left_out</item>
    </style>


right.in的动画

[code]<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate android:fromXDelta="800"
        android:toXDelta="0"
        android:duration="500" />

   <!-- <alpha android:fromAlpha="0.1"
        android:toAlpha="1.0"
        android:duration="800"/>-->

    <!--<scale
        android:fromXScale="0.2"
        android:toXScale="1.0"
        android:fromYScale="0.2"
        android:toYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="500"
        android:interpolator="@android:anim/accelerate_interpolator"/>-->

</set>


left.out.xml的动画

[code]<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate android:fromXDelta="0"
        android:toXDelta="-400"
        android:duration="500" />

   <!-- <alpha android:fromAlpha="1.0"
        android:toAlpha="0.5"
        android:duration="500"/>-->

    <!--<scale
        android:fromXScale="1.0"
        android:toXScale="0.2"
        android:fromYScale="1.0"
        android:toYScale="0.2"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="500"
        android:interpolator="@android:anim/accelerate_interpolator"/>-->

</set>


如果我们希望可以根据屏幕大小设置dialog的宽度。通常这样做,给dialog设置onShowListener



具体里面的实现通常是这样,这个是群里一个朋友的通常写法



ps:其实刚开始想用FragmentDialog实现的,但是很多地方还是不太会处理,放弃了!

欢迎留言讨论!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: