自定义Dialog

来源:互联网 发布:知乎书店办公楼 编辑:程序博客网 时间:2024/06/07 11:53

自定义的Dialog

很多场景中我们都需要用到dialog,当然我们也可以用系统自带的,但是现在的系统被修改得很驳杂,有时候ui很难做到统一样式,不过我们可以通过自定义一些简单的控件,从而在不同的系统上拥有一致的体验,今天是第一次写博客,写一个简单的自定义dialog;

  • 自定义dialog弹窗动画
  • 自定义弹窗样式

自定义dialog弹窗动画

首先在styles.xml文件下创建打开关闭动画样式

    <style name="PopupAnimation1" mce_bogus="1" parent="android:Animation">        <item name="android:windowEnterAnimation">@anim/menuanim1</item>        <item name="android:windowExitAnimation">@anim/menuanim2</item>    </style>

接下来就是创建动画样式 在anim/目录下创建open.xml;close.xml文件,内容分别是

    <?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <alpha        android:duration="700"        android:fromAlpha="0.1"        android:toAlpha="1.0" />    <scale        android:duration="700"        android:fillAfter="false"        android:fromXScale="1.0"        android:fromYScale="0.0"        android:interpolator="@android:anim/accelerate_decelerate_interpolator"        android:pivotX="0%"        android:pivotY="0%"        android:toXScale="1.0"        android:toYScale="1.0" /></set>
    <style name="PopupAnimation1" mce_bogus="1" parent="android:Animation">        <item name="android:windowEnterAnimation">@anim/menuanim1</item>        <item name="android:windowExitAnimation">@anim/menuanim2</item>    </style>
    <?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <alpha        android:duration="700"        android:fromAlpha="1.0"        android:toAlpha="0.1" />    <scale        android:duration="700"        android:fillAfter="false"        android:fromXScale="1.0"        android:fromYScale="1.0"        android:interpolator="@android:anim/accelerate_decelerate_interpolator"        android:pivotX="0%"        android:pivotY="0%"        android:toXScale="1.0"        android:toYScale="0.0" /></set>

以上就是弹窗动画的样式,当然有其他更好的也可以按照自己的想法去做,这里就做一个简单的示例,Android的动画还是满丰富的。

接下来就是

自定义弹窗样式

也是在style文件下创建样式

<style name="mydialog" parent="@android:style/Theme.Dialog">        <item name="android:windowFrame">@null</item>        <!-- 边框 -->        <item name="android:windowIsFloating">true</item>        <!-- 是否浮现在activity之上 -->        <item name="android:windowIsTranslucent">true</item>        <!-- 半透明 -->        <item name="android:windowNoTitle">true</item>        <!-- 无标题 -->        <item name="android:windowBackground">@color/transparent</item>        <!-- 背景透明 -->        <item name="android:backgroundDimEnabled">true</item>        <!-- 模糊 -->    </style>

样式很简单,就是把系统自带的框框去掉,然后修改背景样式。
接下来就是view文件,

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center"    android:orientation="vertical" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="160dp"        android:layout_margin="40dp"        android:background="@drawable/tip_dialog_br"        android:orientation="vertical" >        <TextView            android:id="@+id/title"            android:layout_width="match_parent"            android:layout_height="40dp"            android:gravity="center"            android:padding="8dp"            android:text="提示"            android:textColor="@color/black"            android:textSize="14sp" />        <RelativeLayout            android:layout_width="match_parent"            android:layout_height="80dp" >            <TextView                android:id="@+id/msg"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:layout_centerInParent="true"                android:gravity="center"                android:padding="8dp"                android:text="确认?"                android:textColor="@color/black"                android:textSize="12sp" />        </RelativeLayout>        <View            android:layout_width="match_parent"            android:layout_height="0.5dp"            android:background="@color/grey" />        <LinearLayout            android:id="@+id/button"            android:layout_width="match_parent"            android:layout_height="40dp" >            <Button                android:id="@+id/button1"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:background="@null"                android:text="确认"                android:textColor="@color/main_1"                android:textSize="14sp" />        </LinearLayout>    </LinearLayout></LinearLayout>

以上都是常规的界面了,就不一一说明了,复制代码,粘贴,哪里报错补上去就OK了。没什么难点,其实也就是dailog具体长什么样就是这里设置啦,每个项目需求不一样,比如按钮个数,比如title提示。都可以按自己的想法来;

当然,接下来就是在项目上的应用了。其实就是代码创建一个diglog然后将我们自定义的样式以及view还有动画给这个dialog设置

View mView = LayoutInflater.from(mContext).inflate(                R.layout.tip_dialog_view2, null);        final Dialog BarchDialog = new Dialog(mContext, R.style.mydialog);        if (title != null)            ((TextView) mView.findViewById(R.id.title)).setText(title);        ((TextView) mView.findViewById(R.id.msg)).setText(msg);        mView.findViewById(R.id.button1).setOnClickListener( new OnClickListener() {                    @Override                    public void onClick(View arg0) {                        // TODO Auto-generated method stub                        BarchDialog.cancel();                    }                });        BarchDialog.setContentView(mView);        Window dialogWindow = BarchDialog.getWindow();        dialogWindow.setWindowAnimations(R.style.PopupAnimation1); // 设置窗口弹出动画

好了,调用的时候BarchDialog.show()就可以了,需要复用的话就自己写一个公共方法,入参context返回diglog就可以啦。

好了,就简单介绍到这里,希望可以给大家带来帮助。如果有什么不明白,或者不对的地方还请大家多多指教。

这是我第一次写博客。

原创粉丝点击