2011-11-02 36 views
6

Tôi đã thấy một cái gì đó như thế này:Trường tóm tắt PreferenceCategory thẻ

<PreferenceCategory xmlns:android="http://schemas.android.com/apk/res/android" 
    android:key="vegi_category" android:title="Vegetables" 

    android:summary="Preferences related to vegetable"> <!-- Why is this here? --> 

    <CheckBoxPreference android:key="tomato_selection_pref" 
     android:title="Tomato " android:summary="It's actually a fruit" /> 
    <CheckBoxPreference android:key="potato_selection_pref" 
     android:title="Potato" android:summary="My favorite vegetable" /> 
</PreferenceCategory> 

Nhưng tôi không nhận được nó tại sao lại có một lĩnh vực tóm tắt cho các loại pref:

(android:summary="Preferences related to vegetable")?

Khi tôi sử dụng màn hình pref bản tóm tắt được trình bày trong chế độ xem, nhưng đây không phải là trường hợp có danh mục pref. Sự tồn tại của tóm tắt trong thể loại pref chỉ là một quy ước của nó có thể được nhìn thấy bằng cách nào đó?

Cách sử dụng thực tế tóm tắt trong phần tử danh mục pref là gì?

Trả lời

9

Tiện ích PreferenceCategory chuẩn chỉ hiển thị tiêu đề; thuộc tính android:summary bị bỏ qua.

Đó là bởi vì cách bố trí mặc định (preference_category.xml) chỉ chứa một TextView duy nhất cho lĩnh vực tiêu đề:

<!-- Layout used for PreferenceCategory in a PreferenceActivity. --> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    style="?android:attr/listSeparatorTextViewStyle" 
    android:id="@+android:id/title" 
/> 

Nếu bạn muốn hiển thị tóm tắt là tốt, bạn có thể chỉ định bố trí của riêng bạn bằng cách sử dụng thuộc tính android:layout. Ví dụ:

<PreferenceCategory android:title="Category" android:summary="This is the summary" 
        android:layout="@layout/preference_category_summary"> 

đâu bố trí/preference_category_summary.xml là một cái gì đó như:

<!-- Layout used for PreferenceCategory + SUMMARY in a PreferenceActivity. --> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" android:layout_height="wrap_content" 
       android:orientation="vertical"> 
    <TextView android:id="@+android:id/title" 
       style="?android:attr/listSeparatorTextViewStyle"/> 
    <TextView android:id="@+android:id/summary" 
       android:paddingLeft="5dip" android:paddingRight="dip" 
       android:layout_width="match_parent" android:layout_height="wrap_content"/> 
</LinearLayout> 

Trước khi bạn đi trước và làm điều này, tuy nhiên, bạn nên xem xét cho dù đó là ít hoặc khó hiểu hơn cho người dùng. Trừ khi bạn đánh dấu cẩn thận văn bản tóm tắt, văn bản sẽ nhảy ra khỏi màn hình hoặc xuất hiện để đính kèm với tùy chọn đầu tiên trong danh mục.

7

Bạn chỉ có thể sử dụng Tùy chọn và android: tóm tắt.

<PreferenceCategory xmlns:android="http://schemas.android.com/apk/res/android" 
    android:key="vegi_category" android:title="Vegetables"> 

    <Preference android:summary="Preferences related to vegetable" /> 

    <CheckBoxPreference android:key="tomato_selection_pref" 
     android:title="Tomato " android:summary="It's actually a fruit" /> 
    <CheckBoxPreference android:key="potato_selection_pref" 
     android:title="Potato" android:summary="My favorite vegetable" /> 
</PreferenceCategory> 
+0

hoạt động này. nhưng không hiệu suất rất tốt –

+0

Tôi thấy rằng điều này hoạt động rất tốt (so với thay đổi bố cục; vì nó cần phải phù hợp với giao diện phiên bản Android và cảm nhận quá). Ngoài ra, nếu bạn thêm 'android: selectable = false', bạn có thể đạt được giao diện tương tự. –

0

@ehartwell là hoàn toàn đúng
bố trí của tôi cho các phiên bản khác là:

cho pre-lollipop

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <TextView android:id="@android:id/title" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="6dp" 
    android:textSize="14sp" 
    android:textStyle="bold" 
    android:textAllCaps="true" 
    android:paddingLeft="8dp" 
    android:paddingRight="8dp"/> 

    <TextView android:id="@android:id/summary" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="-5dp" 
    android:textSize="12sp" 
    style="?android:attr/listSeparatorTextViewStyle"/> 

</LinearLayout> 

cho hậu lollipop

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingLeft="@dimen/preference_category_margin" 
    android:paddingRight="@dimen/preference_category_margin" 
    android:orientation="vertical"> 

    <TextView android:id="@android:id/title" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="6dp" 
    android:textStyle="bold" 
    android:textSize="13sp" 
    android:textAllCaps="false" 
    android:textColor="@color/colorAccent"/> 

    <TextView android:id="@android:id/summary" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textSize="12sp" 
    android:textColor="@color/colorAccent" 
    android:textAllCaps="false"/> 

</LinearLayout> 

@ dimen/preferences_category_margin khác với kích thước màn hình đặc biệt
16dp hoặc 24dp
và chúng khá tốt :)

Các vấn đề liên quan