編輯:中級開發
一、常規對話框
public static class MyDialogFragment extends DialogFragment {
int mNum;
static MyDialogFragment newInstance(int num) {
MyDialogFragment f = new MyDialogFragment();
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
下面是Fragment的onCreate方法,需要注意的是,設置布局不在這裡,和Activity有些不同。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments().getInt("num");
int style = DialogFragment.STYLE_NORMAL, theme = 0;
switch ((mNum-1)%6) {
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;
}
switch ((mNum-1)%6) {
case 4: theme = android.R.style.Theme_Light; break;
case 5: theme = android.R.style.Theme; break;
}
setStyle(style, theme);
}
在Fragment設置布局在onCreateVIEw中,使用inflater可以映射一個XML方式布局的layout文件。
@Override
public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container,
Bundle savedInstanceState) {
VIEw v = inflater.inflate(R.layout.fragment_dialog, container, false);
View tv = v.findVIEwById(R.id.text);
((TextVIEw)tv).setText("Dialog #" + mNum + ": using style "
+ getNameForNum(mNum));
Button button = (Button)v.findVIEwById(R.id.show);
button.setOnClickListener(new OnClickListener() {
public void onClick(VIEw v) {
((FragmentDialog)getActivity()).showDialog(); //顯示fragmentdialog
}
});
return v;
}
}
上面的showDialog方法在Activity中的定義如下:
void showDialog() {
mStackLevel++;
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment prev = getFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel);
newFragment.show(ft, "dialog");
}
二、警告對話框,類似AlertDialog
public static class MyAlertDialogFragment extends DialogFragment {
public static MyAlertDialogFragment newInstance(int title) {
MyAlertDialogFragment frag = new MyAlertDialogFragment();
Bundle args = new Bundle();
args.putInt("title", title);
frag.setArguments(args);
return frag;
}
這裡android開發網提示大家,下面重寫了onCreateDialog方法,而不是onCreateVIEw,希望大家注意:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int title = getArguments().getInt("title");
return new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(title)
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((FragmentAlertDialog)getActivity()).doPositiveClick();
}
}
)
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((FragmentAlertDialog)getActivity()).doNegativeClick();
}
}
)
.create();
}
}
顯示Alert的Fragment類似AlertDialog,下面是Activity中使用的showDialog的實現代碼,如下
void showDialog() {
DialogFragment newFragment = MyAlertDialogFragment.newInstance(
R.string.alert_dialog_two_buttons_title);
newFragment.show(getFragmentManager(), "dialog");
}
public void doPositiveClick() {
// Do stuff here.
Log.i("FragmentAlertDialog", "Positive click!");
}
public void doNegativeClick() {
// Do stuff here.
Log.i("FragmentAlertDialog", "Negative click!");
}
如果你不了解Fragment,可以查看android Fragment使用詳解一文
簡介: 對於需要跨應用程序執行期間或生命期而維護重要信息的應用程序來說,能夠在移動設備上本地存儲數據是一種非常關鍵的功能。作為一名開發人員,您經常需要存儲諸如
<?XML version=1.0 encoding=utf-8?> 然後是主布局,一個水平滾動條,放入menu
在Android 3.0中除了我們重點講解的Fragment外,Action Bar也是一個重要的內容,Action Bar主要是用於代替傳統的標題欄,對於androi
(2) RelativeLayout相對布局,它是依靠與父容器,同一容器中其它控件的相對位置來排列顯示的。主要常用的屬性如下:相對父容器的屬性:android:layo