Android之DialogFragment
2016-01-10 13:34
666 查看
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:14px;"> DialogFragment是基于Fragment展示Dialog的窗口。对话框的控制(何时隐藏、显示、关闭)通过对应的Dialogment 的API来完成,而不是Dialog的API.</span></span>
DialogFragment需要保证Fragment和Dialog的状态保持一致。它监听dialog的dissmiss
event,并且去处理自己的状态。这意味着应该调用 show(FragmentManager, String)或者show(FragmentTransaction, String)来添加一个DialogFragment的实例给UI层,并且它自主将remove当dialog消失的时候。
实现这个类创建Dialog,需要通过重写法onCreateView(LayoutInflater,
ViewGroup, Bundle)来填充dialog的内容,或者重写方法onCreateDialog(Bundle)来创建一个完全自定义的dialog。
1. 重写onCreateView创建Dialog
(a)布局文件,一个登陆窗口的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_margin="10dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="姓名:" android:textSize="18sp"/> <EditText android:id="@+id/edit_logiName" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="16sp" android:hint="请输入名字"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_margin="10dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码:" android:textSize="18sp" /> <EditText android:id="@+id/edit_logiPwd" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="16sp" android:hint="请输入密码"/> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/colorPrimary" /> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal"> <Button android:id="@+id/btn_cancel" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="#00000000" android:text="取消"/> <View android:layout_width="1dp" android:layout_height="match_parent" android:background="@color/colorPrimary" /> <Button android:id="@+id/btn_login" android:layout_width="0dp" android:layout_height="match_parent" android:background="#00000000" android:layout_weight="1" android:text="登录"/> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/colorAccent" /> </LinearLayout>(b) 继承DialogFragmeng重写onCreateView方法
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.dia_login, container, false); return view; }
(c)
测试展示
LoginDialog loginDialog = LoginDialog.newInstance(); loginDialog.show(getFragmentManager(), "dilog");
(d) 设置Dialog样式
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mNum = getArguments().getInt("num"); int style = DialogFragment.STYLE_NORMAL, theme = 0; switch (mNum) { case 1: style = DialogFragment.STYLE_NO_TITLE; break; case 2: style = DialogFragment.STYLE_NO_FRAME; break; case 3: style = DialogFragment.STYLE_NO_INPUT; break; case 4: style = DialogFragment.STYLE_NORMAL; break; case 5: style = DialogFragment.STYLE_NORMAL; break; case 6: style = DialogFragment.STYLE_NO_TITLE; break; case 7: style = DialogFragment.STYLE_NO_FRAME; break; case 8: style = DialogFragment.STYLE_NORMAL; break; } switch (mNum) { case 4: theme = android.R.style.Theme_Holo; break; case 5: theme = android.R.style.Theme_Holo_Light_Dialog; break; case 6: theme = android.R.style.Theme_Holo_Light; break; case 7: theme = android.R.style.Theme_Holo_Light_Panel; break; case 8: theme = android.R.style.Theme_Holo_Light; break; }2.重写onCreateDialog方法
(a) DIalog布局同上
(b) 继承DialogFragment,重写OnCreateDialog,创建Dialog
public Dialog onCreateDialog(Bundle savedInstanceState) { LayoutInflater inflater = LayoutInflater.from(getActivity()); View view = inflater.inflate(R.layout.dia_login, null, false); initUI(view); setListener(); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setView(view); return builder.create();(c) 在合适的位置展示Dialog
(d)设置Dialog样式
在XML中定义
<style name="DialogStyle" parent="@android:style/Theme.Dialog"> <item name="android:windowBackground">@drawable/settings_window</item> <item name="android:windowFrame">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:background">@android:color/background_light</item> <item name="android:windowFullscreen">true</item> <item name="android:backgroundDimEnabled">true</item> </style>创建Dialog时调用相应的style
public Dialog onCreateDialog(Bundle savedInstanceState) { ******* AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),R.style.DialogStyle); ****** }
集思广益、坚持不懈的总结,总会进步!
参考资料:
1.http://www.android-doc.com/reference/android/app/DialogFragment.html
2.https://github.com/codepath/android_guides/wiki/Using-DialogFragment
3.http://blog.csdn.net/pipisky2006/article/details/8547539
相关文章推荐
- android 一些总结 1.2-1.10
- Android卡片式列表布局
- B站的DanmakuFlameMaster的使用
- android怎样查看当前project哪些profile是打开的
- Android(Lollipop/5.0) Material Design(四) 创建列表和卡片
- 如何更换Android模拟器界面
- Android应用程序四大组件
- Android开发指南-窗口小部件(App Widgets)
- Android开发指南-三维图形
- Android开发指南-二维图形
- Android开发指南-工具-画九宫格
- android笔记(一)
- android颜色设置
- [置顶] Android性能优化笔记
- Android-ListView卡顿优化
- 深入浅出RxJava四-在Android中使用响应式编程
- Android中四大组件
- 一种android安全机制-数字签名
- Android设计模式源码解析之单例模式
- Android实现图片文字轮播特效