了解Android中的首选项屏幕

时间:2016-08-30 11:13:58

标签: android xml layout preferencescreen

我有一个Settings.xml布局,其中包含按钮,textviews等。我正在努力将其更改为使用PreferenceScreens进行设置布局。我知道我不能像我们在LinearLayout或其他任何方式中使用的那样使用按钮。我的问题如下:

  1. 有没有办法可以使用我的settings.xml中的按钮,视图到我的Preference_settings.xml?
  2. 有没有办法在Settings.xml和Preference_Settings.xml之间建立连接?
  3. 这是我的settings.xml

    <?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="match_parent"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:orientation="vertical"
        android:weightSum="10" >
    
        <RelativeLayout
            android:id="@+id/top_panel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp" >
    
            <Button
                android:id="@+id/select_flight_button"
                android:layout_width="@dimen/settings_button_width"
                android:layout_height="wrap_content"
                android:textColor="@drawable/custom_text_color_for_buttons"
                android:textSize="@dimen/button_text_size"
                android:textStyle="bold" />      
        </RelativeLayout>
    
    
        <LinearLayout
            android:visibility="gone"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="bottom|center_horizontal"
            android:layout_weight="2.25"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="17dp"
            android:gravity="top|center_horizontal"
            android:orientation="horizontal" >
    
            <ImageButton
                android:id="@+id/app_version_tablet_id"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dp"
                android:background="@color/transparent"
                android:gravity="center_horizontal"
                android:src="@drawable/tablet_info" />
    
            <TextView
                android:id="@+id/last_outbound_sync_timestamp"
                style="@style/settings_sync_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="top"
                android:layout_marginTop="10dp"
                android:layout_marginLeft="5dp"
                android:focusable="false"
                android:text="@string/last_sync_time_unknown" />
    
    </LinearLayout>
    

    这是我的preference_settings.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>
            <SwitchPreference
                android:title="Enable Sync" />
    
    </PreferenceScreen>
    

1 个答案:

答案 0 :(得分:0)

如果您想从布局中选择单个视图以添加另一个布局,那么不,您不能这样做。但是,可以在另一个布局中添加整个布局。

preference_settings.xml

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

<!-- Your layout views from settings will start from top place the layout wherever you want-->
<include layout="@layout/settings" />

<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>
    <SwitchPreference
        android:title="Enable Sync" />