这个demo演示了利用DialogFragment来展示和管理各种风格的样式的对话框。
activity_main.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="match_parent"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show"
android:onClick="click"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
对话框的视图界面dialong_content.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="vertical"
>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<Button
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:text="show"/>
</LinearLayout>
MainActivity
public class MainActivity extends Activity {
private int mStackLevel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState!=null){
mStackLevel=savedInstanceState.getInt("level");
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("level", mStackLevel);
}
public void click(View v){
showMyDialog();
}
private void showMyDialog() {
mStackLevel++;
FragmentTransaction ft=getFragmentManager().beginTransaction();
//根据tag查找当前显示的fragment
MyDialogFragment pre=(MyDialogFragment) getFragmentManager().findFragmentByTag("dialog");
if(pre!=null){
//首先移除当前的fragment,尔后再添加新的fragment防止当新的fragment为
//透明样式时前后几个fragment会重叠显示
ft.remove(pre);
}
ft.addToBackStack(null);
//添加新的Fragment
MyDialogFragment newFragment=MyDialogFragment.getInstance(mStackLevel);
//显示DialogFragment并设置tag,以方便找到该组件
newFragment.show(ft, "dialog");
}
public static class MyDialogFragment extends DialogFragment{
private int mNum;
public static MyDialogFragment getInstance(int num){
MyDialogFragment mdf=new MyDialogFragment();
Bundle args=new Bundle();
args.putInt("num", num);
mdf.setArguments(args);
return mdf;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.dialgo_content, container,false);
TextView tv=(TextView) v.findViewById(R.id.text);
String text="Dialog#:"+mNum+"Using style:"+getNameToNum(mNum);
tv.setText(text);
Button show=(Button) v.findViewById(R.id.show);
show.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
((MainActivity)getActivity()).showMyDialog();
}
});
return v;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum=getArguments().getInt("num");
//定制对话框的风格和样式
int style=DialogFragment.STYLE_NORMAL,theme=0;
switch ((mNum-1)%9) {
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-1)%9) {
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;
}
setStyle(style, theme);
}
}
public static String getNameToNum(int num) {
switch ((num-1)%9) {
case 1: return "STYLE_NO_TITLE";
case 2: return "STYLE_NO_FRAME";
case 3: return "STYLE_NO_INPUT (this window can't receive input, so "
+ "you will need to press the bottom show button)";
case 4: return "STYLE_NORMAL with dark fullscreen theme";
case 5: return "STYLE_NORMAL with light theme";
case 6: return "STYLE_NO_TITLE with light theme";
case 7: return "STYLE_NO_FRAME with light theme";
case 8: return "STYLE_NORMAL with light fullscreen theme";
}
return "STYLE_NORMAL";
}
}