2016-02-04 27 views
5

Ekran preferencji systemu Android na Lollipop wygląda tak, jakby korzystał z układu podobnego do "układu karty".Jak uzyskać wygląd systemu Android?

enter image description here

Próbuję uzyskać ten sam wygląd dla mojej aplikacji, ale nie widzę, w jaki sposób osiągnąć to domyślny PreferenceFragment. Czy ktoś może mi wytłumaczyć, w jaki sposób mogę osiągnąć ten sam efekt bez konieczności pisania własnych preferencji?


Zauważ, że widziałem i czytać te pytania, ale nie zapewniają one odpowiednią odpowiedź.


Jeśli jest to interesujące, moje obecne preferencje xml wygląda następująco:

<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> 

I przedłużyć PreferenceFragment i załadować xml poprzez

addPreferencesFromResource(R.xml.prefs); 
+0

nie mają żadnych api> 20 urządzenie pod ręką, nie patrząc na próbie to za pomocą monitora urządzenia -> hierarchia widoku Dump? –

+0

@DavidMedenjak Robiam to, co daje mi hierarchię układów. Co mam z tym zrobić? Jak już powiedziałem, nie zamierzam odtwarzać struktury układu w normalnym układzie, ale raczej chcę, aby mój istniejący 'PreferenceFragment' wyglądał tak samo. – Baz

Odpowiedz

4

ja "rozwiązany", dodając manekina Preference między przedmiotami PreferenceCategory, który wykorzystuje układ, który zmienia wygląd ekranu w pytaniu:

<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> 

z układem, który wygląda mniej więcej tak:

<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

Jak zdefiniowałeś shadow_top i shadow_bottom? –

+0

@JavierDelgado Są to tylko obrazy ([9-patch] (https://developer.android.com/studio/write/draw9patch.html)). – Baz

+0

Czy masz szansę podzielić się nimi? – JayTee

0

Dla mnie to nie działa, aby ustawić tło pośrednictwem niestandardowy układ. Również wysokość nie ma zastosowania.

który pracował dla mnie jednak:

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 zdjęć krosowe tutaj: https://drive.google.com/file/d/1M5uXXnFc0HHx2BOHQqLtvAXFet4ICDj0/view?usp=sharing