![](/img/trans.png)
[英]Android: Custom layout for DialogFragment is not showing fully in it's width like AlertDialog
[英]DialogFragment : Using AlertDialog with custom layout
我正在使用Fragments API
支持重寫我的應用程序。 在原始應用程序中,我有一個AlertDialog
:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.button_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(view);
ListView mListView = (ListView) view.findViewById(R.id.mblist);
builder.setTitle(getString(R.string.sysinfo)).setNeutralButton(
getString(R.string.okay), null);
Cursor dcursor = propManager.propCursor();
if (dcursor.moveToFirst()) {
startManagingCursor(dcursor);
String[] from = { Constants.NAME_DET,
Constants.VALUE_DET };
int[] to = { R.id.mbname, R.id.mbvalue };
SimpleCursorAdapter dadapter = new SimpleCursorAdapter(this,
R.layout.info_list_row, dcursor, from, to);
mListView.setAdapter(dadapter);
}
final Dialog dialog = builder.create();
dialog.show();
如何通過DialogFragment顯示相同的對話框? 我已閱讀文檔,但無法弄清楚如何繼續這樣做。
我很驚訝沒有人回答。 這是解決方案:
public class PropDialogFragment extends DialogFragment {
private PropDialogFragment() { /*empty*/ }
/** creates a new instance of PropDialogFragment */
public static PropDialogFragment newInstance() {
return new PropDialogFragment();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//getting proper access to LayoutInflater is the trick. getLayoutInflater is a //Function
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.my_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(view);
builder.setTitle(getActivity().getString(R.string.sysinfo)).setNeutralButton(
getActivity().getString(R.string.okay), null);
return builder.create();
}
}
您可以使用以下方式顯示對話框
private void showDialog() {
// DialogFragment.show() will take care of adding the fragment
// in a transaction. We also want to remove any currently showing
// dialog, so make our own transaction and take care of that here.
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
// Create and show the dialog.
DialogFragment newFragment = PropDialogFragment.newInstance();
newFragment.show(ft, "dialog");
}
基本上你可以像這樣顯示你的片段:
SherlockDialogFragment newFragment = new TimePickerFragment();
newFragment.show(getActivity().getSupportFragmentManager(), "timePicker");
這將是一個簡單的TimePickerFragment:
public class TimePickerFragment extends SherlockDialogFragment implements TimePickerDialog.OnTimeSetListener{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.