2013-08-01 12 views
8

mam PreferenceScreen zawierają wiele opcji, to i dostosować ją skierować ją do niestandardowy układ jak poniżej:android zestaw dzielnik wyściółka na ekranie preferencji

<?xml version="1.0" encoding="utf-8" ?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> 
    <CheckBoxPreference 
    android:summary="checkbox one" 
    android:key="key_one" 
    android:layout="@layout/mylayout"  
    /> 
    <CheckBoxPreference 
    android:summary="checkbox two" 
    android:key="key_two" 
    android:layout="@layout/mylayout"  
    /> 
</PreferenceScreen> 

mylayout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="?android:attr/listPreferredItemHeight" 
    android:gravity="center_vertical" 
    android:paddingRight="?android:attr/scrollbarSize"> 
<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:minWidth="@dimen/preference_icon_minWidth" 
    android:orientation="horizontal"> 
<ImageView 
    android:id="@+android:id/icon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" /> 
</LinearLayout> 
<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="16dip" 
    android:layout_marginRight="8dip" 
    android:layout_marginTop="6dip" 
    android:layout_marginBottom="6dip" 
    android:layout_weight="1"> 
<TextView 
    android:id="@+android:id/title" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:fadingEdge="horizontal" /> 
<TextView 
    android:id="@+android:id/summary" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@android:id/title" 
    android:layout_alignLeft="@android:id/title" 
    android:textAppearance="?android:attr/textAppearanceSmall" 
    android:textColor="?android:attr/textColorSecondary" 
    android:maxLines="4" /> 
</RelativeLayout> 
<LinearLayout 
    android:id="@+android:id/widget_frame" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:minWidth="@dimen/preference_widget_width" 
    android:gravity="center" 
    android:orientation="vertical" /> 
</LinearLayout> 

Każdego wszystko działa dobrze, a także dostosowuję dzielnik między polami wyboru w aktywności prefs jako:

ListView list = (ListView) findViewById(android.R.id.list); 
    list.setDivider(new ColorDrawable(Color.RED)); // or some other color int 
    list.setDividerHeight((5)); 
    list.setVerticalScrollBarEnabled(false); 

gdy chcę ustawić dopełnienie do dzielnika jedynie jako:

list.setPadding(0, 0, 0, 0); 

to ustawić dopełnienie wszystkich widzenia

Jak mogę ustawić dopełnienie do dzielnika tylko bez wpłynąć na dopełnienie wszystkich widzenia.

Każda pomoc będzie mile widziany, dzięki

Odpowiedz

4

Aby umożliwić preferencji Przegródka wyściółka

pierwszy: dodaj tę linię do aktywności preferencji który doprowadzi do przejrzystości Androida domyślny dzielnik

list.setDivider(new ColorDrawable(0x00000000)); 

sekund: Utwórz folder o nazwie rozciągliwej w OZE, a następnie utworzyć na nim divider.xml co będzie jak poniżej:

<?xml version="1.0" encoding="utf-8" ?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="#B22222" /> 
</shape> 

trzeci:

dodać Widok do swojej mylayout.xml więc będzie jak poniżej:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:minHeight="?android:attr/listPreferredItemHeight" 
android:gravity="center_vertical" 
android:paddingRight="?android:attr/scrollbarSize"> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:minWidth="@dimen/preference_icon_minWidth" 
    android:orientation="horizontal"> 
    <ImageView 
     android:id="@+android:id/icon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     /> 
</LinearLayout> 

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="16dip" 
    android:layout_marginRight="8dip" 
    android:layout_marginTop="6dip" 
    android:layout_marginBottom="6dip" 
    android:layout_weight="1"> 

    <TextView android:id="@+android:id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"    
     android:textAppearance="?android:attr/textAppearanceMedium"   
     android:fadingEdge="horizontal" /> 

    <TextView android:id="@+android:id/summary" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@android:id/title" 
     android:layout_alignLeft="@android:id/title" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:textColor="?android:attr/textColorSecondary" 
     android:maxLines="4" /> 

    <View 
     android:id="@+id/divider" 
     android:background="@drawable/divider" 
     android:layout_width="match_parent" 
     android:layout_marginLeft="30dp" 
     android:layout_marginRight="30dp" 
     android:layout_height="5dp" 
     android:layout_below="@+android:id/summary"/> 

</RelativeLayout> 

<!-- Preference should place its actual preference widget here. --> 
<LinearLayout android:id="@+android:id/widget_frame" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:minWidth="@dimen/preference_widget_width" 
    android:gravity="center" 
    android:orientation="vertical" /> 

punktu tutaj dodaj Widok poniżej swoich tekstów i przekierowuj ten widok do kształtu dzielnika w resorcie do rysowania, aby w końcu uzyskać niestandardowy dzielnik, który można dostosować w razie potrzeby.

nadzieję, że ci pomogą.

wyjście będzie jak poniżej:

enter image description here

+0

Witam, staram się usunąć linii podziału w moim preferencji fragmentu. Ale nie dostaję jak zadzwonić 'ListView list = (ListView) findViewById (android.R.id.list);' Nie mam obiektu widoku w części – Naruto

1

Trzeba utworzyć niestandardowe odkształcalne-shape

custom_divider.xml

<shape android:shape="rectangle"> 
    <solid android:color="@color/grey" /> 
    <corners android:radius="2dp" /> 
</shape> 

ten zestaw jako div IDER do ListView w xml

activity_list.xml

<ListView 
    android:dividerHeight="2dp" 
    android:divider="@drawable/custom_divider" 
    ... 
/> 
+0

nie mam ListView, nazywamy ListView jako android.R.id.list aktywności aby umożliwić dostosować rozdzielacz na ekranie preferencji, dzięki – androidqq6

1

Musisz utworzyć własny zasób rozciągliwej dla dzielnika:

<?xml version="1.0" encoding="utf-8"?> 

<inset xmlns:android="http://schemas.android.com/apk/res/android" android:insetTop="5dp"> 

    <shape android:shape="rectangle"> 

     <solid android:color="#FF0000" /> 

     <size android:height="4dp" /> 

    </shape> 

</inset> 

w tym przypadku dzielnik kształt wysokość jest 4DP i top wyściółka jest 5DP , kiedy ustawiasz wysokość dzielnika, musisz zsumować wysokość kształtu + wstawkę.

ListView list = (ListView) findViewById(android.R.id.list); 
list.setDivider(new ColorDrawable(Color.RED)); // or some other color int 
list.setDividerHeight((5)); //wrorng code 
list.setVerticalScrollBarEnabled(false); 

Aby ustawić wysokość dzielnik poprawnie na wszystkich ekranach dencity należy ustawić go w DP ale domyślnym numerem „5” interpretowane w pikselach (px).

final float scale = getContext().getResources().getDisplayMetrics().density; 
int pixels = (int) (dps * scale + 0.5f); 

Aby uzyskać więcej informacji na temat kształtu i wstawka wykorzystania zasobów odkształcalne Android Dokumentacji Drawable resources

1

Należy starać uzyskiwania motyw z domyślnego androida, przypisując je do ekranu preferencji (choć oni mówią, że to błąd na 2 .x wersje, które zapobiega temu, że jeśli obejść jak opisano here) i przesłanianie właściwość jednego ze stylów android:dividerPadding.

naprawdę nie mogę powiedzieć na pewno, jaki styl byłoby, trzeba kopać w nim, oto Android styles.xml celach informacyjnych i oto przykład how to override ListView appearance in a theme

Powiązane problemy