简体   繁体   中英

How to create headers identical to PreferenceFragment category titles?

I'm writing my first Android application at the moment and I'm trying to maintain consistency. I've already implemented my own style of header for grouping input fields:

在此输入图像描述

Now I've just implemented my PreferenceFragments and they put my header to shame! Is there a way to duplicate these headers automagically created by Android for the preference category titles?

在此输入图像描述

Is this a built in widget, or is it just text styling with a separate View for the bar as I've done in my original header? If it is just styling, how do I achieve an identical look and feel?

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/header_my_tasks"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/content_main_heading_my_tasks"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:layout_alignParentRight="true"
        android:layout_alignBottom="@id/header_my_tasks"
        android:text="Last updated 26/01/2014 15:11:04"/>
    <View
        android:id="@+id/divider_my_tasks"
        android:background="#CCC"
        android:layout_width="fill_parent"
        android:layout_height="2dip"
        android:layout_weight="0"
        android:layout_below="@id/header_my_tasks" />

</RelativeLayout>

The layout file for preference category in called preferences_category_holo.xml (see Android Source code). It uses style, which is called Widget.TextView.ListSeparator . Attribute is called listSeparatorTextViewStyle . I assume, if you apply this style to your TextView on the left hand side, then you should see same design. HEre is the style declaration from Android's styles.xml file.

<style name="Widget.TextView.ListSeparator">
    <item name="android:background">@android:drawable/dark_header_dither</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textColor">?textColorSecondary</item>
    <item name="android:textSize">14sp</item>
    <item name="android:gravity">center_vertical</item>
    <item name="android:paddingStart">8dip</item>
</style>

I managed to work it out myself in the end. Just apply the following style to a TextView:

style="?android:attr/listSeparatorTextViewStyle"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM