2015-10-06 13 views
6

Sto sviluppando un'applicazione per la progettazione di materiali.Perché il layout settings.xml si sovrappone a ActionBar/Barra degli strumenti?

Dopo aver aggiunto settings.xml in SettingsActivity.java & in esecuzione l'applicazione, l'attività sta comparendo in questo modo:

enter image description here

Ecco il codice SettingsActivity.java del file:

public class SettingsActivity extends AppCompatActivity { 

    Toolbar toolbar; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_settings); 

     SpannableString s = new SpannableString("Settings"); 
     s.setSpan(new TypefaceSpan(this, "Roboto-Medium.ttf"), 0, s.length(), 
       Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setTitle(s); 

     // Display the fragment as the main content. 
     getFragmentManager().beginTransaction() 
       .replace(android.R.id.content, new SettingsFragment()) 
       .commit(); 

    } 

    public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener { 

     public static final String SHARE_APP_PREFERENCE_KEY = "pref_key_share_app"; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      // Load the preferences from an XML resource 
      addPreferencesFromResource(R.xml.settings); 

     } 

     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, 
               String key) { 
      if (key.equals(SHARE_APP_PREFERENCE_KEY)) { 
       // do something 
      } 
     } 

    } 

} 

Ecco il codice activity_settings.xml del file :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:tools="http://schemas.android.com/tools" 
       xmlns:app="http://schemas.android.com/apk/res-auto" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       tools:context="com.abc.xxx.SettingsActivity"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:layout_scrollFlags="scroll|enterAlways" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
     android:elevation="4dp"/> 

</RelativeLayout> 

Ecco il codice del file settings.xml:

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

    <PreferenceCategory android:title="@string/pref_notification" > 
     <CheckBoxPreference 
      android:defaultValue="true" 
      android:key="prefNotification" 
      android:summary="@string/pref_notify_summary" > 
     </CheckBoxPreference> 
    </PreferenceCategory> 

</PreferenceScreen> 

Non so il motivo per cui il layout settings.xml si sovrappone l'ActionBar/Toolbar!

Per favore fatemi sapere.

Grazie in anticipo.

risposta

9

Cambia la tua activity_settings.xml a questo codice

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:tools="http://schemas.android.com/tools" 
        xmlns:app="http://schemas.android.com/apk/res-auto" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        tools:context="com.abc.xxx.SettingsActivity"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|enterAlways" 
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
      android:elevation="4dp"/> 

    <FrameLayout 
     android:id="@+id/fragment_container" 
     android:layout_width="match_parent" 
     android:layout_below="@id/toolbar" 
     android:layout_height="match_parent" /> 
    </RelativeLayout> 

E il cambiamento a questo

// Display the fragment as the main content. 
    getFragmentManager().beginTransaction() 
      .replace(R.id.fragment_container, new SettingsFragment()) 
      .commit(); 
+0

Come posso cambiare il colore del testo del titolo della preferenza? –

+1

Puoi cercare in questo link http://stackoverflow.com/questions/6297635/custom-preferencecategory-headings – Sunny

+0

Perché devo inserire '' sotto la barra degli strumenti? – RoCk

1

scoperto che PreferenceActivity in soluzione di sole sopra potrebbe bloccarsi a causa di carica in vista dei contenuti, senza un controllo ListView. Questo problema viene corretto aggiungendolo a activity_settings.xml, ma PreferenceFragment lo ignorerà. L'activity_settings.xml sarà l'aspetto qualcosa di simile

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<include layout="@layout/common_actionbar" /> 

<ListView 
    android:id="@android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

<FrameLayout 
    android:id="@+id/fragment_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

2

Quando si chiama il frammento sull'attività, non utilizzare

// Display the fragment as the main content. 
    getFragmentManager().beginTransaction() 
      .replace(android.R.id.content, new SettingsFragment()) 
      .commit(); 

Invece utilizzare

// Display the fragment as the main content. 
    getFragmentManager().beginTransaction() 
      .replace(R.id.Your_Frame_ID_On_xml, new SettingsFragment()) 
      .commit(); 

Ho avuto lo stesso problema con l'utilizzo di android.R.id.content, quindi cambiandolo all'ID che si usa per il frame d il frammento sul xml lo ha risolto.

Problemi correlati