Cómo establecer el título de DialogFragment?
Esto debería ser una tarea simple, pero por alguna razón puedo encontrar una manera de establecer el título de un DialogFragment. (Estoy configurando el contenido del diálogo usando onCreateView
sobrecarga).
El estilo predeterminado deja un lugar para el título, pero no puedo encontrar ningún método en la clase DialogFragment
para establecerlo.
El título se establece de alguna manera mágicamente cuando se usa el método onCreateDialog
para establecer el contenido, así que me pregunto si esto es por diseño, o hay un truco especial para establecerlo cuando se usa la sobrecarga onCreateView
.
5 answers
Puedes usar getDialog().setTitle("My Dialog Title")
Así:
public static class MyDialogFragment extends DialogFragment {
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Set title for this dialog
getDialog().setTitle("My Dialog Title");
View v = inflater.inflate(R.layout.mydialog, container, false);
// ...
return v;
}
// ...
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-19 14:17:51
¿Funciona sobreescribir onCreateDialog
y establecer el título directamente en el Dialog
? Así:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setTitle("My Title");
return dialog;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-03-04 13:22:48
La respuesta de Jason solía funcionar para mí, pero ahora necesita las siguientes adiciones para obtener el título para mostrar.
En primer lugar, en el método onCreate()
de su MyDialogFragment, agregue:
setStyle(DialogFragment.STYLE_NORMAL, R.style.MyDialogFragmentStyle);
Entonces, en tus estilos.archivo xml, añadir:
<style name="MyDialogFragmentStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">false</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">false</item>
</style>
Después de horas de probar cosas diferentes, este es el único que ha hecho el truco para mí.
NB - Es posible que necesite cambiar el Theme.AppCompat.Light.Dialog.Alert
a otra cosa para que coincida con el estilo de su tema.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 10:31:26
Puedes echar un vistazo a los documentos oficiales. La forma en que lo hice es así:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
.setTitle("My Title");
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.my_layout, null);
builder.setView(view);
return builder.create();
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-08 12:39:45
DialogFragment podría representarse como diálogo y como Actividad. Utilice el código a continuación que funcionaría correctamente para ambos
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (getShowsDialog()) {
getDialog().setTitle(marketName);
} else {
getActivity().setTitle(marketName);
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-17 02:35:00