自定义Dialog&DialogFragment

DialogFragment

这是个展示dialog窗口的fragment。这个fragment包含了一个Dialog对象,它的展示是基于fragment的状态。控制dialog(决定要show,hide,dismiss等)应该通过dialogfragment的api而不是dialog的

实现这个类需要通过override方法onCreateView(LayoutInflater, ViewGroup, Bundle)来填充dialog的内容。另外,可以override方法onCreateDialog(Bundle)来创建一个完全自定义的dialog。


生命周期:

DialogFragment做了很多事情来控制fragment的生命周期,而不是dialog的。注意dialog是完全自主的,它有自己的window,接收自己的输入事件,经常自己决定什么时候消失(通过back键或者用户点击dialog上的button)。

DialogFragment需要保证Fragment和Dialog的状态保持一致。它监听dialog的dissmiss event,并且去处理自己的状态。这意味着应该调用 show(FragmentManager, String)或者show(FragmentTransaction, String)来添加一个DialogFragment的实例给UI层,并且它自主将remove当dialog消失的时候。


DialogFragment说到底还是个fragment,它爹能干的事情它也都能做,所以也可以是 通过ft.addToBackStack(null)压倒栈中,通过back键逐个resume,也可以被嵌套到更大层的UI中。

下面是个简单的dialog fragment:

public static class MyDialogFragment extends DialogFragment {
    static MyDialogFragment newInstance() {
        return new MyDialogFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.hello_world, container, false);
        View tv = v.findViewById(R.id.text);
        ((TextView)tv).setText("This is an instance of MyDialogFragment");
        return v;
    }
}
下面是按照dialog形式来显示:

void showDialog() {
    // Create the fragment and show it as a dialog.
    DialogFragment newFragment = MyDialogFragment.newInstance();
    newFragment.show(getFragmentManager(), "dialog");
}

下面是添加到另一个view布局中

FragmentTransaction ft = getFragmentManager().beginTransaction();
DialogFragment newFragment = MyDialogFragment.newInstance();
ft.add(R.id.embedded, newFragment);
ft.commit();

看了百度新闻的dialog和自身UI框架是挺一致的。下面是根据它的样式的模拟的代码。

先看看style

<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>

Style:是一个包含一种或者多种格式化属性的集合,我们可以将其用为一个单位用在布局XML单个元素当中。比如,我们可以定义一种风格来定义文本的字号大小和颜色,然后将其用在View元素的一个特定的实例。
Theme:是一个包含一种或者多种格式化属性的集合,我们可以将其为一个单位用在应用中所有的Activity当中或者应用中的某个Activity当 中。比如,我们可以定义一个Theme,它为window frame和panel 的前景和背景定义了一组颜色,并为菜单定义可文字的大小和颜色属性,可以将这个Theme应用在你程序当中所有的Activity里。

定义Style和Theme我觉得主要是可重用性


dialog的自定义的view

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="300.0dip"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/title_text_view"
        android:layout_width="300.0dip"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="10.0dip"
        android:layout_marginTop="20.0dip"
        android:gravity="center_horizontal"
        android:text="@string/dialog_default_title"
        android:textColor="@color/dialog_title_text_color"
        android:textSize="16.0sp" />

    <TextView
        android:id="@+id/content_text_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="20.0dip"
        android:layout_marginLeft="15.0dip"
        android:layout_marginRight="15.0dip"
        android:gravity="left"
        android:text="@string/dialog_latest_prompt"
        android:textColor="@color/dialog_content_text_color"
        android:textSize="16.0sp" />

    <View
        android:layout_width="300.0dip"
        android:layout_height="1.0dip"
        android:background="#ffdddddd" />

    <LinearLayout
        android:id="@+id/option_bar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/dialog_btn_bg_color"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/dialog_ok"
            android:layout_width="0.0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:background="@drawable/dialog_btn_selector"
            android:gravity="center"
            android:paddingBottom="14.0dip"
            android:paddingTop="14.0dip"
            android:text="@string/dialog_ok"
            android:textColor="@color/dialog_content_text_color"
            android:textSize="18.0sp" />

        <View
            android:layout_width="1.0dip"
            android:layout_height="fill_parent"
            android:background="#ffdddddd" />

        <TextView
            android:id="@+id/dialog_cancel"
            android:layout_width="0.0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:background="@drawable/dialog_btn_selector"
            android:gravity="center"
            android:paddingBottom="14.0dip"
            android:paddingTop="14.0dip"
            android:text="@string/dialog_cancel"
            android:textColor="@color/dialog_content_text_color"
            android:textSize="18.0sp" />
    </LinearLayout>

    <TextView
        android:id="@+id/ok_text_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/dialog_btn_selector"
        android:gravity="center"
        android:paddingBottom="14.0dip"
        android:paddingTop="14.0dip"
        android:text="@string/dialog_ok_label"
        android:textColor="@color/dialog_content_text_color"
        android:textSize="18.0sp"
        android:visibility="gone" />

</LinearLayout>


DialogFragment

  public static class CustomDialogFragment extends DialogFragment{
	  
	  public static interface DialogClickListener{
		  public void doPositiveClick();
		  public void doNegativeClick();
	  }
	  
	  static DialogClickListener mListener;
	  
	  public CustomDialogFragment(){
		 
	  }
	  
	  public static CustomDialogFragment newInstance(String title, String message, DialogClickListener listener){
		  CustomDialogFragment frag = new CustomDialogFragment();
		  Bundle b = new Bundle();
		  b.putString("title", title);
		  b.putString("message", message);
		  frag.setArguments(b);		  
		  mListener = listener;
		  
		  return frag;
	  }
	  
	  
      @Override
		public Dialog onCreateDialog(Bundle savedInstanceState) {
			final Dialog dialog = new Dialog(getActivity(), R.style.DialogStyle);

			LayoutInflater inflater = (LayoutInflater) getActivity()
					.getSystemService(LAYOUT_INFLATER_SERVICE);
			View view = inflater.inflate(R.layout.dialog_layout, null, false);

			String title = getArguments().getString("title");
			String message = getArguments().getString("message");
			if (title != null && title.length() > 0) {
				TextView t = (TextView) view.findViewById(R.id.title_text_view);
				t.setText(title);
			}

			if (message != null && message.length() > 0) {
				TextView m = (TextView) view
						.findViewById(R.id.content_text_view);
				m.setText(message);
			}

			View ok = view.findViewById(R.id.dialog_ok);
			View cancel = view.findViewById(R.id.dialog_cancel);

			ok.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					dialog.dismiss();
					if (mListener != null) {
						mListener.doPositiveClick();
					}

				}

			});

			cancel.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					dialog.dismiss();
					if (mListener != null) {
						mListener.doNegativeClick();
					}
				}

			});

			dialog.setContentView(view);

			return dialog;
		}
  }


在合适的实际调用showDialog

    	CustomDialogFragment newFragment = CustomDialogFragment.newInstance("title", "message", null);
        newFragment.show(getSupportFragmentManager(), "dialog");



  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值