2016-07-14 8 views
0

Ich möchte meinen Titel in meinem DialogFragment anpassen, ich weiß, wie ich den Titel zu meinem DialogFragment setze, aber ich will mehr nicht nur einen Titel, ich brauche einen benutzerdefinierten Titel (Farbe, fontSize ...), So definiert ich eine Textview und maßgeschneiderte esWie man Titel in DialogFragment anpasst

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.height_fragment,container,false); 

    TextView title = new TextView(getContext()); 
    title.setText("Your Height Information"); 
    title.setGravity(Gravity.CENTER); 
    title.setTextSize(30); 
    title.setBackgroundColor(Color.GRAY); 
    title.setTextColor(Color.WHITE); 

    getDialog().setCustomTitle(title); //Error Can not resolve method setCustomTitle(android.widget.TextView) 

    return view; 
} 

aber es ist ein Fehler, auf dieser Linie

getDialog().setCustomTitle(title); 

getDialog() setCustomTitle (Titel).

Ich habe viel gesucht, bevor ich diese Frage gepostet habe, aber ich konnte nicht finden, wie ich meinen Titel von DialogFragment anpassen kann. Vielen Dank für Ihre Zeit, um mir zu helfen

+0

Sie müssen die Textview von Ihrem Layout verweisen. Ich poste eine Sekunde Code – bradkratky

+0

Haben Sie diese Lösung ausprobiert? (http://stackoverflow.com/questions/28977576/dialogfragment-with-a-custom-title) –

+0

Die Methode ist getDialog(). setTitle (String title). http://stackoverflow.com/questions/5193722/how-to-set-the-title-of-dialogfragment Erste Antwort auf Google. –

Antwort

0

Ich könnte nie einfach den Titel meiner DialogFragment anpassen. Stattdessen entferne ich den Titel und füge ein TextView-Layout-Layout hinzu (und verwende es wie einen Titel). Auf diese Weise können Sie anpassen, wie Sie möchten und einfach.

Dialog-Code:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    // Remove TITLE 
    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); 


    View dialogView = dialogView = inflater.inflate(R.layout.height_fragment, null); 

    // Do your stuff 

    return dialogView; 
} 

height_fragment.xml

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TextView 
     android:id="@+id/dialog_title" 
     android:layout_width="match_parent" 
     android:layout_height="56dp" 

     <!-- THIS VIEW IS YOUR TITLE... CUSTOMIZE LYKE YOU WANT --> 
     android:textColor="@color/white" 
     android:textSize="18sp" 
     android:textStyle="bold" 
     android:layout_gravity="top" 

     android:text="@string/TITLE" 
     android:gravity="center_vertical" 
     android:paddingLeft="16dp" /> 

    <!-- Real Content goes below the title --> 
</FrameLayout> 
0

Sie benötigen xml Ihrer Klasse zu verbinden. findViewById teilt Ihrer Klasse mit, welche TextView verwendet werden soll. Beachten Sie, wie es die android:id im xml übereinstimmen müssen:

public class DialogFragmentTest extends DialogFragment { 

    public final static String TITLE = "title"; 
    public final static String MESSAGE = "message"; 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     // Get the layout inflater 
     LayoutInflater inflater = getActivity().getLayoutInflater(); 
     View view = inflater.inflate(R.layout.dialog, null); 

     // Inflate and set the layout for the dialog 
     // Pass null as the parent view because its going in the dialog layout 
     builder.setView(view); 

     Bundle args = getArguments(); 
     String title = args.getString(TITLE, "default title"); 
     String msg = args.getString(MESSAGE, "default message"); 

     TextView tvTitle = (TextView)view.findViewById(R.id.dialog_title); 
     tvTitle.setText(title); 

     TextView tvMessage = (TextView)view.findViewById(R.id.dialog_message); 
     tvMessage.setText(msg); 

     Button btnDone = (Button)view.findViewById(R.id.dialog_button); 
     btnDone.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       dismiss(); 
      } 
     }); 

     return builder.create(); 
    } 
} 

dialog.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="300dp" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:id="@+id/dialog_title" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:text="title"/> 
    <TextView 
     android:id="@+id/dialog_message" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/dialog_title" 
     android:layout_marginTop="10dp" 
     android:text="message"/> 
    <Button 
     android:id="@+id/dialog_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/dialog_message" 
     android:layout_marginTop="20dp" 
     android:layout_centerHorizontal="true" 
     android:text="done"/> 


</RelativeLayout> 

Dies ist dann von Ihrer Aktivität aufgerufen wird mit:

DialogFragmentTest bd = new DialogFragmentTest(); 
Bundle b = new Bundle(); 
b.putString(DialogFragmentTest.TITLE, "my title"); 
b.putString(DialogFragmentTest.MESSAGE, "my message"); 
bd.setArguments(b); 
bd.show(getFragmentManager(), "my title"); 
0

"getDialog" nicht habe eine Methode "setCustomTitle". Diese Methode kann nur für AlertDialog.Builder verwendet werden. Probieren Sie es wie folgt aus:

getDialog().setTitle("Your Height Information"); 
TextView textView=(TextView) getDialog().findViewById(android.R.id.title); 
textView.setTextSize(30); 
0

Bitte verwenden Sie benutzerdefinierte Dialog, basierend auf DialogFragment:

public class MyCustomDialog extends DialogFragment{ 
    private Context context; 

    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     this.context = context; 
    } 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 
     View dialogView = inflater.inflate(R.layout.custom_dialog, null, false); 
     TextView tvTitle = (TextView) getDialog().findViewById(android.R.id.title); 
     tvTitle.setTextSize(25); 
     tvTitle.setTextColor(Color.GREEN); 

     return dialogView; 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 

     Dialog dialog = new Dialog(this.context, R.style.CustomDialog); 
     dialog.setTitle("custom dialog title"); 
     dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); 
     return dialog; 
    } 

    public static MyCustomDialog newInstance() { 

     MyCustomDialog myCustomDialog = new MyCustomDialog(); 
     return myCustomDialog; 
    } 
} 
Verwandte Themen