Android的DialogFragment的简单使用。
2016-12-10 18:25
330 查看
先看文章
Android ListView入门知识–各种Adapter配合使用
Android–>Dialog/DialogFragment宽度高度修改/全屏,自定义样式
Android 自定义 DialogFragment 宽度问题
[Android]ListView中分割线的设置
(转)Android 开发 对话框Dialog dismiss和hide方法的区别
Android ListView 除去边缘阴影、选中色、拖动、底部上拉、顶部下拉背景色等
Android Dialog 系统样式讲解及透明背景
看看activity_main.xml 点击弹出dialog
dialog_fragment.xml 实现listview布局
listitem.xml 每个子项的布局
MyDialogFragment,写一个dialogfragment。SimpleAdapter填入数据。
MainActivity,点击按钮弹出dialog。
完。
Android ListView入门知识–各种Adapter配合使用
Android–>Dialog/DialogFragment宽度高度修改/全屏,自定义样式
Android 自定义 DialogFragment 宽度问题
[Android]ListView中分割线的设置
(转)Android 开发 对话框Dialog dismiss和hide方法的区别
Android ListView 除去边缘阴影、选中色、拖动、底部上拉、顶部下拉背景色等
Android Dialog 系统样式讲解及透明背景
看看activity_main.xml 点击弹出dialog
<?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:padding ="10dp" > <Button android:id ="@+id/button1" android:layout_width ="match_parent" android:layout_height ="wrap_content" android:text ="点击" /> </LinearLayout>
dialog_fragment.xml 实现listview布局
<?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" an ce91 droid:layout_marginLeft ="5dp" android:layout_marginRight ="5dp" android:layout_centerVertical="true" > <ListView android:id ="@+id/list" android:scrollbars="none" android:layout_width ="match_parent" android:layout_height ="match_parent" android:divider="#dddddd" android:fadingEdge="none" android:background="@drawable/btn" android:overScrollMode="never" android:dividerHeight="2dp" /> </LinearLayout>
listitem.xml 每个子项的布局
<?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:orientation="horizontal"> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:src="@mipmap/ic_launcher" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="22sp" android:text="@string/app_name" /> <TextView android:id="@+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="13sp" android:text="@string/app_name"/> </LinearLayout> </LinearLayout>
MyDialogFragment,写一个dialogfragment。SimpleAdapter填入数据。
public class MyDialogFragment extends DialogFragment implements OnItemClickListener { private ListView mylist; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.dialog_fragment, null, false); mylist = ( ListView ) view.findViewById(R.id.list); getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);//去掉dialog的标题 //透明度 WindowManager.LayoutParams lp=getDialog().getWindow().getAttributes(); lp.alpha=0.8f; getDialog().getWindow().setAttributes(lp); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); SimpleAdapter adapter = new SimpleAdapter(getActivity(),getData(),R.layout.listitem, new String[]{"title","info","img"}, new int[]{R.id.title,R.id.info,R.id.img}); mylist.setAdapter(adapter); mylist.setOnItemClickListener( this ); } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // dismiss(); } // 重写onstart方法 @Override public void onStart() { super.onStart(); Dialog dialog = getDialog(); if (dialog != null) { DisplayMetrics dm = new DisplayMetrics(); getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm); // 设置dialog的高度和宽度 dialog.getWindow().setLayout((int) (dm.widthPixels * 0.9), (int) (dm.heightPixels * 0.75)); } } private List<Map<String, Object>> getData() { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map = new HashMap<String, Object>(); map.put("title", "不可能的梦"); map.put("info", "别只顾着吃"); map.put("img",R.mipmap.a); list.add(map); map = new HashMap<String, Object>(); map.put("title", "好想你想"); map.put("info", "厉害了"); map.put("img", R.mipmap.a); list.add(map); map = new HashMap<String, Object>(); map.put("title", "有你正好"); map.put("info", "下班回家,买菜做饭"); map.put("img", R.mipmap.tu); list.add(map); map = new HashMap<String, Object>(); map.put("title", "勇敢的心"); map.put("info", "今天天气不错"); map.put("img",R.mipmap.w); list.add(map); map = new HashMap<String, Object>(); map.put("title", "就知道你不会相信"); map.put("info", "别打扰我!"); map.put("img", R.mipmap.a); list.add(map); map = new HashMap<String, Object>(); map.put("title", "梦见蝴蝶"); map.put("info", "老...老板,来..来一碗面。"); map.put("img",R.mipmap.w); list.add(map); return list; } }
MainActivity,点击按钮弹出dialog。
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showDialog(view); } }); } public void showDialog(View view) { FragmentManager manager = getFragmentManager(); MyDialogFragment dialog = new MyDialogFragment(); //dialog.show(manager, "dialog");参数为 :一个布局,字符串(可以为空)。 dialog.show(manager,""); } }
完。
相关文章推荐
- android DialogFragment简单使用
- Android中DialogFragment的简单使用及常见问题
- android FragmentTabHost 的简单使用
- Android中使用DialogFragment来取代popopwindow
- Android 使用DialogFragment 对话框实现
- Android:使用fragment(碎片)—让你的应用更灵活(一)简单运用
- Android官方推荐使用DialogFragment替换AlertDialog
- android菜鸟学习笔记27----Fragment的简单使用
- Android的DialogFragment的基本使用方法
- Android 屏幕旋转(方向和configChanges) 处理 AsyncTask 和 ProgressDialog 的最佳方案(DialogFragment 使用 续)
- android中dialog的简单使用
- android DialogFragment简单应用
- android Fragment的简单使用
- Android fragment的简单使用使用和通信
- android DialogFragment使用 附例子
- Android:DialogFragment的使用
- android 中Fragment 的简单使用
- Android 关于ViewPager结合碎片Fragment的简单使用
- Android官方推荐使用DialogFragment替换AlertDialog
- android中fragment简单使用以及fragment之间数据交互