PreferenceActivity把PreferenceScree嵌套在其他布局中


原文来自:点击打开链接

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

 

Java代码   收藏代码
  1. <ListView  
  2.       android:id="@android:id/list"  
  3.       android:layout_width="match_parent"  
  4.       android:layout_height="match_parent" />  

 作为占位符 就可一了

PreferenceScreen 布局保持不变

Java代码   收藏代码
  1. <PreferenceScreen  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:title="@string/settings">  
  4.     <PreferenceCategory  
  5.         android:title="@string/clock_settings">  
  6.         <!-- <ListPreference  
  7.             android:key="clock_style"  
  8.             android:title="@string/clock_style"  
  9.             android:entries="@array/clock_style_entries"  
  10.             android:entryValues="@array/clock_style_values"  
  11.             android:defaultValue="@string/default_clock_style"  
  12.             android:dialogTitle="@string/clock_style" /> -->  
  13.         <CheckBoxPreference  
  14.             android:key="automatic_home_clock"  
  15.             android:title="@string/automatic_home_clock"  
  16.             android:summary="@string/automatic_home_clock_summary"  
  17.             android:defaultValue="true" />  
  18.         <ListPreference  
  19.             android:key="home_time_zone"  
  20.             android:entries="@array/timezone_labels"  
  21.             android:entryValues="@array/timezone_values"  
  22.             android:dialogTitle="@string/home_time_zone_title"  
  23.             android:title="@string/home_time_zone" />  
  24.     </PreferenceCategory>  
  25.   
  26.     <PreferenceCategory  
  27.         android:title="@string/alarm_settings">  
  28.         <ListPreference  
  29.             android:key="auto_silence"  
  30.             android:title="@string/auto_silence_title"  
  31.             android:entries="@array/auto_silence_entries"  
  32.             android:entryValues="@array/auto_silence_values"  
  33.             android:defaultValue="10"  
  34.             android:dialogTitle="@string/auto_silence_title" />  
  35.   
  36.        <com.haier.deskclock.SnoozeLengthDialog  
  37.             android:key="snooze_duration"  
  38.             android:title="@string/snooze_duration_title"  
  39.             android:defaultValue="10"/>  
  40.   
  41.         <VolumePreference  
  42.             android:title="@string/alarm_volume_title"  
  43.             android:dialogTitle="@string/alarm_volume_title"  
  44.             android:persistent="false"  
  45.             android:streamType="alarm" />  
  46.   
  47.         <ListPreference  
  48.             android:key="volume_button_setting"  
  49.             android:title="@string/volume_button_setting_title"  
  50.             android:dialogTitle="@string/volume_button_setting_title"  
  51.             android:entries="@array/volume_button_setting_entries"  
  52.             android:entryValues="@array/volume_button_setting_values"  
  53.             android:defaultValue="0" />  
  54.     </PreferenceCategory>  
  55. </PreferenceScreen>  

 上面是settings.xml

下面是我最终想要显示的程序

Java代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="@drawable/setting_bg"  
  6.     android:orientation="horizontal">  
  7.       
  8.    <RelativeLayout android:layout_width="160dip"  
  9.         android:layout_height="match_parent"  
  10.         android:background="@drawable/toolbarbg"  
  11.         android:paddingTop="30dip">  
  12.           
  13.         <ImageView  android:layout_width="wrap_content"  
  14.             android:layout_height="wrap_content"  
  15.             android:id="@+id/toolbar_screen_home"  
  16.             android:layout_alignParentTop="true"  
  17.             android:background="@drawable/toolbar_home" />  
  18.         <ImageView  android:layout_width="wrap_content"  
  19.             android:layout_height="wrap_content"  
  20.             android:id="@+id/toolbar_screen_back"  
  21.             android:layout_below="@id/toolbar_screen_home"  
  22.             android:background="@drawable/toolbar_back" />  
  23.           
  24.         <ImageView  android:layout_width="wrap_content"  
  25.             android:layout_height="wrap_content"  
  26.             android:id="@+id/toolbar_setting"  
  27.             android:layout_alignParentBottom="true"  
  28.             android:background="@drawable/toolbar_home" />  
  29.           
  30.         <ImageView  android:layout_width="wrap_content"  
  31.             android:layout_height="wrap_content"  
  32.             android:id="@+id/toolbar_add"  
  33.             android:layout_above="@id/toolbar_setting"  
  34.             android:background="@drawable/toolbar_home" />  
  35.           
  36.      </RelativeLayout>  
  37.        
  38.      <LinearLayout android:layout_width="match_parent"  
  39.         android:layout_height="match_parent"  
  40.         android:orientation="vertical">  
  41.           
  42.           <LinearLayout android:layout_width="match_parent"  
  43.               android:layout_height="130dip"  
  44.               android:background="@drawable/titlebar"  
  45.               android:paddingTop="15dip"  
  46.               android:paddingLeft="30dip"  
  47.               android:orientation="vertical">  
  48.                 
  49.               <TextView  android:layout_width="wrap_content"  
  50.                   android:layout_height="wrap_content"  
  51.                   android:text="Setting - Alarm"  
  52.                   android:gravity="left"  
  53.                   android:textColor="#fdb200"  
  54.                   android:textSize="30sp"  
  55.                   android:id="@+id/setting_text"/>  
  56.                 
  57.               <TextView  android:layout_width="wrap_content"  
  58.                   android:layout_height="wrap_content"  
  59.                   android:text="设置 - 闹钟"  
  60.                   android:shadowColor="#000000"  
  61.                   android:shadowDy="4"  
  62.                   android:gravity="left"  
  63.                   android:textColor="#fff6ce"  
  64.                   android:textSize="30sp"/>  
  65.           
  66.           </LinearLayout>  
  67.             
  68.           <LinearLayout android:layout_width="match_parent"  
  69.               android:layout_height="match_parent"  
  70.               android:orientation="vertical">  
  71.                 
  72.              <ListView  
  73.         android:id="@android:id/list"  
  74.         android:layout_width="match_parent"  
  75.         android:layout_height="match_parent" />  
  76.           
  77.           </LinearLayout>  
  78.             
  79.            
  80.           
  81.      </LinearLayout>  
  82.           
  83.       
  84.   
  85. </LinearLayout>  

 一定要注意listview 还有他的id

然后调用

Java代码   收藏代码
  1. addPreferencesFromResource(R.xml.settings);  
  2. setContentView(R.layout.setting_main);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值