[英]Creating custom preference screen in android
I have created a PreferenceScreen(pref_screen.xml) for my settings menu. 我已经为我的设置菜单创建了一个PreferenceScreen(pref_screen.xml)。 It contains list of objects with different categories. 它包含具有不同类别的对象列表。 I have created another custom layout(settings.xml) containing buttons, views, texts, images. 我创建了另一个自定义布局(settings.xml),其中包含按钮,视图,文本,图像。
I would like to know how can I display the contents of my custom layout in my PreferenceScreen. 我想知道如何在PreferenceScreen中显示自定义布局的内容。
This is my pref_screen.xml 这是我的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 这是我的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. 之后,使用findViewById
访问customlayout的元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.