2016-02-04 19 views
5

La schermata delle preferenze di sistema di Android su lecca-lecca sembra utilizzare un layout che assomiglia al "layout della carta".Come visualizzare le preferenze di sistema Android?

enter image description here

che sto cercando di ottenere lo stesso aspetto per la mia app, ma non può vedere come si è raggiunto con il default PreferenceFragment. Qualcuno può spiegarmi come posso ottenere lo stesso effetto senza dover scrivere le mie preferenze personali?


Si noti che ho visto e letto queste domande, ma non forniscono una risposta adeguata:.


Se è di interesse, il mio attuale preferenze XML assomiglia a questo:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:custom="http://schemas.android.com/apk/res-auto"> 

    <PreferenceCategory android:title="@string/preferences_title_general"> 
     <Preference 
      android:key="Prefs.Delete.Row.Token" 
      android:summary="@string/preferences_delete_row_token_summary" 
      android:title="@string/preferences_delete_row_token_title"/> 
     <Preference 
      android:key="Prefs.Delete.Cell.Token" 
      android:summary="@string/preferences_delete_cell_token_summary" 
      android:title="@string/preferences_delete_cell_token_title"/> 
     <Preference 
      android:key="Prefs.Null.Token" 
      android:summary="@string/preferences_null_token_summary" 
      android:title="@string/preferences_null_token_title"/> 
    </PreferenceCategory> 
    <PreferenceCategory android:title="@string/preferences_title_appearance"> 
     <SwitchPreference 
      android:dialogTitle="@string/preferences_theme_title" 
      android:key="Prefs.Appearance.Theme" 
      android:summaryOff="@string/preferences_theme_summary_off" 
      android:summaryOn="@string/preferences_theme_summary_on" 
      android:switchTextOff="@string/general_no" 
      android:switchTextOn="@string/general_yes" 
      android:title="@string/preferences_theme_title"/> 
    </PreferenceCategory> 
    <PreferenceCategory android:title="@string/preferences_title_misc"> 
     <SwitchPreference 
      android:dialogTitle="@string/preferences_read_back_title" 
      android:key="Prefs.Voice.Feedback" 
      android:switchTextOff="@string/general_no" 
      android:switchTextOn="@string/general_yes" 
      android:title="@string/preferences_read_back_title"/> 
     <Preference 
      android:key="Prefs.Export.Barcode.Properties" 
      android:title="@string/preferences_export_title" 
      android:summary="@string/preferences_export_summary"/> 
     <Preference 
      android:key="Prefs.Show.Eula" 
      android:title="@string/preferences_eula_title" 
      android:summary="@string/preferences_eula_summary"/> 
    </PreferenceCategory> 

</PreferenceScreen> 

ed estendo PreferenceFragment e caricare il file XML tramite

addPreferencesFromResource(R.xml.prefs); 
+0

non hanno alcun api 20 dispositivo> a portata di mano, hai provato guardando con il monitor del dispositivo -> Dump vista gerarchia? –

+0

@DavidMedenjak Ciò mi dà una gerarchia di layout. Ora cosa faccio con quello? Come ho detto, non sto cercando di riprodurre la struttura del layout in un layout normale, ma preferisco che il mio preferenceFragment esistente sia lo stesso. – Baz

risposta

4

Ho "risolto", questo con l'aggiunta un manichino Preference tra gli elementi PreferenceCategory che utilizza un layout che mima l'aspetto nello screenshot nella domanda:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:custom="http://schemas.android.com/apk/res-auto"> 
    <PreferenceCategory></PreferenceCategory> 

    <Preference layout="@layout/preference_divider" /> 

    <PreferenceCategory></PreferenceCategory> 
</PreferenceScreen> 

Con un layout che sembra qualcosa di simile:

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

    <View  android:layout_width="match_parent" 
       android:layout_height="5dp" 
       android:background="@drawable/shadow_bottom" /> 

    <View  android:layout_width="match_parent" 
       android:layout_height="5dp" 
       android:background="@drawable/shadow_top" /> 

</LinearLayout> 
+0

Come hai definito shadow_top e shadow_bottom? –

+0

@JavierDelgado Sono solo immagini ([9 patch] (https://developer.android.com/studio/write/draw9patch.html)). – Baz

+0

Qualche possibilità di condividerle? – JayTee

0

Per me non ha funzionato per impostare uno sfondo tramite un layout personalizzato. Anche l'altezza non si applicava.

che ha funzionato per me, però:

public class PreferenceDivider extends Preference { 
    public PreferenceDivider(Context context) { 
     super(context); 
    } 
    public PreferenceDivider(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 
    @Override 
    protected View onCreateView(ViewGroup parent) { 
     Resources res=getContext().getResources(); 
     View v=super.onCreateView(parent); 
     v.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, res.getDimensionPixelSize(R.dimen.divider_height))); 
     v.setBackground(res.getDrawable(R.drawable.preference_category_divider, null)); 
     return v; 
    } 
} 

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:custom="http://schemas.android.com/apk/res-auto"> 
    <PreferenceCategory/>> 

    <your.package.path.PreferenceDivider/> 

    <PreferenceCategory/>> 

</PreferenceScreen> 

9 immagini di patch sono qui: https://drive.google.com/file/d/1M5uXXnFc0HHx2BOHQqLtvAXFet4ICDj0/view?usp=sharing