最近心情不太好,原因是被一女女抛弃了,作为一名程序屌丝,我深感无助,从来没有任何的要求
啰嗦了几句,众屌原谅。
先上效果图:
大概就是这个屌样,
重写了Dialog,主要就是头尾部,可自定义,容易扩展,可设置动画。
上代码(声明:代码没有经过最优化处理,众屌可以自己做处理)
layout文件,(心情不好,懒得整合)
文件名:dialog_c.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/WHITE" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:id="@+id/dialog_title_textView1" android:layout_width="match_parent" android:layout_height="40dp" android:layout_gravity="center" android:layout_weight="1" android:background="@color/DIALOG_HEADER_BG_COLOR" android:gravity="center" android:text="Medium Text" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="@color/BLACK" /> <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="1px" android:background="@color/DIALOG_DIVIDER_COLOR" /> <LinearLayout android:id="@+id/main" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:orientation="vertical" > </LinearLayout> <ImageView android:id="@+id/imageView2" android:layout_width="match_parent" android:layout_height="1px" android:background="@color/DIALOG_DIVIDER_COLOR" /> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:layout_weight="1" android:orientation="horizontal" > <Button android:id="@+id/dialog_button_confirm" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/dialog_button_selector" android:text="@string/dialog_confirm" android:textColor="@color/BLACK" /> <ImageView android:id="@+id/imageView1" android:layout_width="1px" android:layout_height="match_parent" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:background="@color/DIALOG_DIVIDER_COLOR" /> <Button android:id="@+id/dialog_button_cancel" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/dialog_button_selector" android:text="@string/dialog_cancel" /> </LinearLayout> </LinearLayout>
Java文件:CDialog.java(我都忘了为毛会起这么挫的名字)
包名什么的自己改就行了。
ScreenConfig 类是屏幕宽高,
package com.chishacai.widgets; import android.app.Activity; import android.app.Dialog; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; import com.chishacai.personalcenter.R; import com.chishacai.personalcenter.config.ScreenConfig; public class CDialog extends Dialog { private Activity act; private Window window; private LinearLayout main; private TextView title; private Button confirm; private Button cancel; private CInterface cInterface; private CDialog instance; public CDialog(Activity act) { super(act, R.style.GeneralDialog); this.act = act; setContentView(R.layout.dialog_c); window = getWindow(); // 得到对话框 settingDialog(window); initWidgets(); instance = this; } private void initWidgets() { title = (TextView) window.findViewById(R.id.dialog_title_textView1); confirm = (Button) window.findViewById(R.id.dialog_button_confirm); confirm.setTag(0); confirm.setOnClickListener(click); cancel = (Button) window.findViewById(R.id.dialog_button_cancel); cancel.setTag(1); cancel.setOnClickListener(click); main = (LinearLayout) window.findViewById(R.id.main); } private View.OnClickListener click = new View.OnClickListener() { @Override public void onClick(View v) { int tag = Integer.valueOf(v.getTag().toString()); switch (tag) { case 0: if (cInterface != null) { cInterface.confirm(); } instance.dismiss(); break; case 1: if (cInterface != null) { cInterface.cancel(); } instance.dismiss(); break; } } }; /** * set title * * @author ddJuly * @date 2013-11-1 下午3:19:35 * @param text */ public void setTitle(String text) { title.setText(text); } /** * set title * * @author ddJuly * @date 2013-11-1 下午3:19:44 * @param text */ public void setTitle(int text) { title.setText(text); } /** * set content view * * @author ddJuly * @date 2013-11-1 下午3:19:49 * @param view */ public void setView(View view) { main.addView(view); } /** * set interface * * @author ddJuly * @date 2013-11-1 下午3:19:57 * @param cInterface */ public void setCInterface(CInterface cInterface) { this.cInterface = cInterface; } public interface CInterface { void confirm(); void cancel(); } public static void settingDialog(Window window) { WindowManager.LayoutParams wl = window.getAttributes(); // 根据x,y坐标设置窗口需要显示的位置 wl.x = 0; // x小于0左移,大于0右移 wl.y = 0; // y小于0上移,大于0下移 wl.width = ScreenConfig.screenWidth - 100; // wl.alpha = 0.6f; //设置透明度 // wl.gravity = Gravity.BOTTOM; //设置重力 window.setAttributes(wl); window.setWindowAnimations(R.style.popwin_anim_style); // 设置窗口弹出动画 } }
public class ScreenConfig { public static int screenWidth; public static int screenHeigh; }
values文件夹下的color.xml(我都忘了用了那些颜色了)
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="BG_COLOR">#FFFFFF</color> <color name="WHITE">#FFFFFF</color> <color name="BLACK">#000000</color> <color name="GRAY">#CCCCCC</color> <color name="MAIN_GREEN">#409d23</color> <color name="GENERAL_ITEM_STROKE_COLOR">#999999</color> <color name="GENERAL_ITEM_STROKE_DOWN_COLOR">#D0F5C1</color> <color name="TITLE_BAR_BG">#409d23</color> <color name="LOGIN_BUTTON_COLOR">#4aac2c</color> <color name="LOGIN_FINDPASS_COLOR">#66c7ff</color> <color name="LOGIN_REGISTER_COLOR">#e8b61e</color> <color name="EDITINFO_GRAY_COLOR">#CCCCCC</color> <color name="EDITINFO_LEFT_TEXT_COLOR">#e8b61e</color> <color name="EDITINFO_RIGHT_TEXT_COLOR">#66c7ff</color> <color name="DIALOG_DIVIDER_COLOR">#CCCCCC</color> <color name="DIALOG_HEADER_BG_COLOR">#e7e6e6</color> <color name="DIALOG_BUTTON_KEYDOWN_COLOR">#CCCCCC</color> <color name="SETTING_EXIT_BUTTON1">#CC0000</color> <color name="SETTING_EXIT_BUTTON2">#990000</color> <color name="GENERAL_GREEN_BUTTON_UP">#409d23</color> <color name="GENERAL_GREEN_BUTTON_DOWN">#32781B</color> <color name="GENERAL_BLUE_BUTTON_UP">#66c7ff</color> <color name="GENERAL_BLUE_BUTTON_DOWN">#29B3FD</color> <color name="GENERAL_YELLOW_BUTTON_UP">#e8b61e</color> <color name="GENERAL_YELLOW_BUTTON_DOWN">#D4A416</color> </resources>
values下的style.xml添加
<style name="GeneralDialog" parent="@android:Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@android:color/white</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item> </style> <style name="popwin_anim_style"> <item name="android:windowEnterAnimation">@anim/menushow</item> <item name="android:windowExitAnimation">@anim/menuhide</item> </style>
anim文件夹下:
文件:menushow.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator" > <translate android:fromXDelta="0" android:toXDelta="0" android:fromYDelta="-500" android:toYDelta="0" android:duration="500" /> <alpha android:fromAlpha="0.1" android:toAlpha="1.0" android:duration="500"/> </set>
文件:menuhide.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="0" android:fromYDelta="0" android:toYDelta="-500" android:duration="500" /> <alpha android:fromAlpha="1.0" android:toAlpha="0.1" android:duration="500"/> </set>
使用方法:(基本跟那个AlertDialog一样)
// create nickname dialog nameDialog = new CDialog(this); View nnView = inflater.inflate(R.layout.dialog_nickname_c, null); final TextView nameText = (TextView) nnView.findViewById(R.id.editText1); nameText.setText(nickname); nameDialog.setView(nnView); nameDialog.setTitle(R.string.editinfo_nickname); nameDialog.setCInterface(new CDialog.CInterface() { @Override public void confirm() { nickname = nameText.getText().toString(); nicknameView.setText(nickname); } @Override public void cancel() { } });
如有改进或错误之处,不怜赐教。