您的位置:首页 > 移动开发 > Android开发

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

<?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,"");
}
}


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