简体   繁体   中英

Creating custom preference screen in android

I have created a PreferenceScreen(pref_screen.xml) for my settings menu. It contains list of objects with different categories. I have created another custom layout(settings.xml) containing buttons, views, texts, images.

I would like to know how can I display the contents of my custom layout in my PreferenceScreen.

This is my pref_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">


    <PreferenceCategory
        android:title="Flight Selection" >

        <Preference
            android:title="Change Flight"
            android:selectable="true"
            android:key="@string/choose_flight"
            android:icon="@drawable/icon_select_flight" />
        <Preference
            android:title="Close Flight"
            android:selectable="true"
            android:key="@string/close_flight_successful"
            android:icon="@drawable/icon_select_flight" />

    </PreferenceCategory>

    <PreferenceCategory
        android:title="Terminal Panel" >

        <Preference
            android:title="Change Terminal"
            android:selectable="true"
            android:icon="@drawable/icon_terminal" />

    </PreferenceCategory>

    <PreferenceCategory
        android:title="Printer Panel" >

        <Preference
            android:title="Choose Printer"
            android:icon="@drawable/icon_printer" />

    </PreferenceCategory>

    <PreferenceCategory
        android:title="AirFi Sync" >

        <SwitchPreference
            android:title="Enable Sync" />

        <Preference
            android:title="Sync Group"
            android:icon="@drawable/airfi" />

        <Preference
            android:title="Sync Details"
            android:icon="@drawable/airfi" />

    </PreferenceCategory>


</PreferenceScreen>

And this is my settings.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/settings_content"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:orientation="vertical"
    android:weightSum="10" >



        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:id="@+id/relativeLayout3">

        </RelativeLayout>
        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="@color/transparent"
            android:layout_below="@+id/relativeLayout2"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:id="@+id/view" />

        <Button
            android:layout_margin="10dp"
            android:id="@+id/select_flight_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true" />


        <Button
            android:layout_margin="10dp"
            android:id="@+id/close_leg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <Button
            android:layout_margin="10dp"
            android:visibility="visible"
            android:id="@+id/configure_printer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/white_button_bg_state_list"
            android:drawableLeft="@drawable/icon_printer"
            android:maxHeight="@dimen/global_button_height"
            android:minHeight="@dimen/global_button_height"
            android:text="@string/choose_printer"
            android:textColor="@drawable/custom_text_color_for_buttons"
            android:textSize="@dimen/button_text_size"
            android:textStyle="bold"
            android:layout_below="@+id/textView4"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <Button
            android:layout_margin="10dp"
            android:visibility="visible"
            android:id="@+id/configure_print_format"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@id/configure_printer"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:background="@drawable/white_button_bg_state_list"
            android:drawableLeft="@drawable/icon_printer"
            android:maxHeight="@dimen/global_button_height"
            android:minHeight="@dimen/global_button_height"
            android:text="@string/change_printer_format"
            android:textColor="@drawable/custom_text_color_for_buttons"
            android:textSize="@dimen/button_text_size"
            android:textStyle="bold" />

        <Button
            android:layout_margin="10dp"
            android:visibility="visible"
            android:id="@+id/configure_payment_device"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/white_button_bg_state_list"
            android:drawableLeft="@drawable/icon_terminal"
            android:maxHeight="@dimen/global_button_height"
            android:minHeight="@dimen/global_button_height"
            android:text="@string/change_payment_device"
            android:textColor="@drawable/custom_text_color_for_buttons"
            android:textSize="@dimen/button_text_size"
            android:textStyle="bold"
            android:layout_below="@+id/textView3"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:textStyle="bold"
            android:textSize="20sp"
            android:text="Terminal Panel"
            android:textColor="@color/adyen_green"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/close_leg"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:id="@+id/textView3" />

        <TextView
            android:textStyle="bold"
            android:textSize="20sp"
            android:text="Printing Panel"
            android:textColor="@color/adyen_green"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/configure_payment_device"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:id="@+id/textView4" />


    </RelativeLayout>


    <View
        android:id="@+id/divider_net_sales"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="5dp"
        android:background="@color/transparent" />




    <RelativeLayout
        android:id="@+id/ifs_panel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:gravity="bottom">

        <View
            android:id="@+id/divider_ifs_panel"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:visibility="gone"
            android:background="@color/transparent" />

        <TextView
            android:id="@+id/sync"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_below="@id/divider_ifs_panel"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:text="@string/airfi_sync"
            android:textSize="18sp"
            android:visibility="gone"
            android:textStyle="bold" />

    </RelativeLayout>

</LinearLayout>

set layout for Individual preferenceScreenusing this :

<Preference
    android:title="@string/label_pref_version"
    android:key="@string/pref_version"
    android:layout="@layout/pref" />

After this Use findViewById to access element of customlayout.

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