2016-10-27 5 views
-1

Ich habe von Actionbar to Toolbar wechseln!Symbolleiste ändern Textansicht Titel

Zur besseren Entwurf habe ich die Schrift meiner Toolbar ändern mit einem Textview und diesen Code

Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar); 
TextView toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title); 
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/NewFont.ttf"); 
toolbarTitle.setTypeface(myTypeface); 

Kann jetzt auf alle Einstellungen Fragment Seiten der gleichen Texteinstellungen ist

Ich habe Test ist mit diesem

private void setTitleText(String Title) { 

    Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar); 
    TextView toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title); 
    Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/NewFont.ttf"); 
    toolbarTitle.setText(Title); 
    toolbarTitle.setTypeface(myTypeface); 
} 

Und hier eines der Fragment Segment

public static class GeneralPreferenceFragment extends PreferenceFragment { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     addPreferencesFromResource(R.xml.pref_general); 
    //with this i call the title change 
     setTitleText(R.string.pref_header_general); 

     setHasOptionsMenu(true); 

     // Bind the summaries of EditText/List/Dialog/Ringtone preferences 
     // to their values. When their values change, their summaries are 
     // updated to reflect the new value, per the Android Design 
     // guidelines. 
     bindPreferenceSummaryToValue(findPreference("example_text")); 
     bindPreferenceSummaryToValue(findPreference("example_list")); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 
     if (id == android.R.id.home) { 
      startActivity(new Intent(getActivity(), SettingsActivity.class)); 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

und diese Arbeit nicht, dass ich diese Fehlermeldung muß

Non-static method 'setTitleText(java.lang.String)' cannot be referenced from a static context 

bearbeiten

ich gefunden habe, einen einfachen Weg

ich ändere diesen

Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar); 
TextView toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title); 
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/NewFont.ttf"); 
toolbarTitle.setTypeface(myTypeface); 

TextView toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title); 
    Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/NewFont.ttf"); 
    toolbarTitle.setText(toolbar.getTitle()); 
    toolbarTitle.setTypeface(myTypeface); 
    getSupportActionBar().setDisplayShowTitleEnabled(false); 

Und jetzt funktioniert alles Feine

Antwort

-1

Sie können nicht static Methode innerhalb static Methode/Klasse nennen nicht. Sie müssen entfernen ‚static‘ aus dem Fragment:

public static class GeneralPreferenceFragment extends PreferenceFragment 
+0

Diese Arbeits nicht Testen Sie selbst ist Dies ist meine Fehlermeldung bei der statischen innere Klasse Dieses Fragment entfernen sollte statisch sein (com.test.beta. SettingsActivity.GeneralPreferenceFragment) –

+0

Warum haben Sie die setTitleText-Methode im Fragment aufgerufen? Ich schlage vor, Sie rufen es in Ihrer MainActivity –

+0

Ich rufe es auf, weil er wissen sollte, welches Fragment aufgerufen wurde. Du kannst mal in Android Studio die Einstellungsvorlage anschauen. Und da ich eine Textansicht verwende, ändert er leider nicht den Titel selbst auf der Werkzeugleiste. –

Verwandte Themen