PreferenceActivity下嵌套PreferenceScreen在其它布局中

今天在修改系统代码的时候,系统代码用了PreferenceActivity,出于某种需求,我想把布局PreferenceScreen整个的嵌套在我的一个正常的layout xml中,就好像把PreferenceScreen 作为一个普通的view使用如button一样随便放,开始以为不可行 后来终于找到了替换办法 那就是 把要PreferenceScreen嵌入的地方用一个 

<ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

 作为占位符 就可一了

PreferenceScreen 布局保持不变

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:title="@string/settings">
    <PreferenceCategory
        android:title="@string/clock_settings">
        <!-- <ListPreference
            android:key="clock_style"
            android:title="@string/clock_style"
            android:entries="@array/clock_style_entries"
            android:entryValues="@array/clock_style_values"
            android:defaultValue="@string/default_clock_style"
            android:dialogTitle="@string/clock_style" /> -->
        <CheckBoxPreference
            android:key="automatic_home_clock"
            android:title="@string/automatic_home_clock"
            android:summary="@string/automatic_home_clock_summary"
            android:defaultValue="true" />
        <ListPreference
            android:key="home_time_zone"
            android:entries="@array/timezone_labels"
            android:entryValues="@array/timezone_values"
            android:dialogTitle="@string/home_time_zone_title"
            android:title="@string/home_time_zone" />
    </PreferenceCategory>

    <PreferenceCategory
        android:title="@string/alarm_settings">
        <ListPreference
            android:key="auto_silence"
            android:title="@string/auto_silence_title"
            android:entries="@array/auto_silence_entries"
            android:entryValues="@array/auto_silence_values"
            android:defaultValue="10"
            android:dialogTitle="@string/auto_silence_title" />

       <com.haier.deskclock.SnoozeLengthDialog
            android:key="snooze_duration"
            android:title="@string/snooze_duration_title"
            android:defaultValue="10"/>

        <VolumePreference
            android:title="@string/alarm_volume_title"
            android:dialogTitle="@string/alarm_volume_title"
            android:persistent="false"
            android:streamType="alarm" />

        <ListPreference
            android:key="volume_button_setting"
            android:title="@string/volume_button_setting_title"
            android:dialogTitle="@string/volume_button_setting_title"
            android:entries="@array/volume_button_setting_entries"
            android:entryValues="@array/volume_button_setting_values"
            android:defaultValue="0" />
    </PreferenceCategory>
</PreferenceScreen>

 上面是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:background="@drawable/setting_bg"
    android:orientation="horizontal">
    
   <RelativeLayout android:layout_width="160dip"
        android:layout_height="match_parent"
        android:background="@drawable/toolbarbg"
        android:paddingTop="30dip">
        
        <ImageView  android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/toolbar_screen_home"
            android:layout_alignParentTop="true"
            android:background="@drawable/toolbar_home" />
        <ImageView  android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/toolbar_screen_back"
            android:layout_below="@id/toolbar_screen_home"
            android:background="@drawable/toolbar_back" />
        
        <ImageView  android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/toolbar_setting"
            android:layout_alignParentBottom="true"
            android:background="@drawable/toolbar_home" />
        
        <ImageView  android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/toolbar_add"
            android:layout_above="@id/toolbar_setting"
            android:background="@drawable/toolbar_home" />
        
     </RelativeLayout>
     
     <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        
          <LinearLayout android:layout_width="match_parent"
              android:layout_height="130dip"
              android:background="@drawable/titlebar"
              android:paddingTop="15dip"
              android:paddingLeft="30dip"
              android:orientation="vertical">
              
              <TextView  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="Setting - Alarm"
                  android:gravity="left"
                  android:textColor="#fdb200"
                  android:textSize="30sp"
                  android:id="@+id/setting_text"/>
              
              <TextView  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="设置 - 闹钟"
                  android:shadowColor="#000000"
                  android:shadowDy="4"
                  android:gravity="left"
                  android:textColor="#fff6ce"
                  android:textSize="30sp"/>
        
          </LinearLayout>
          
          <LinearLayout android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">
              
             <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
        
          </LinearLayout>
          
         
        
     </LinearLayout>
        
    

</LinearLayout>

 一定要注意listview 还有他的id

然后调用

addPreferencesFromResource(R.xml.settings);
        setContentView(R.layout.setting_main);

相关推荐