PreferenceActivity中使用layout 布局文件

PreferenceActivity保存配置信息,固然很好用,但不能满足设计的要求,比如在PreferenceActivity中添加按钮。使用自定义Preference控件可以满足要求,直接使用l布局文件效果更好.效果图

效果图

1.配置Preference.xml文件

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

2.底部button按钮的layout文件 ,其中layout文件必须包含listview控件,且ID为list。

  <? 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:orientation =" vertical " >
  <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:drawSelectorOnTop="false" /> 
- < LinearLayout style =" @android:style/ButtonBar " android:orientation =" horizontal " android:layout_width =" match_parent " android:layout_height =" wrap_content " xmlns:android =" http://schemas.android.com/apk/res/android " >
  < Button android:id =" @+id/button1 " android:layout_height =" wrap_content " android:layout_width =" 140dip " android:ellipsize =" marquee " android:singleLine =" true " android:text =" @string/enable_profile " />
<!--
 Placeholder to get blank space between the two buttons 
  -->
  < View android:visibility =" invisible " android:layout_height =" 0dip " android:layout_width =" 1dip " android:layout_weight =" 1 " />
  < Button android:id =" @+id/button2 " android:layout_height =" wrap_content " android:layout_width =" 140dip " android:drawablePadding =" 3dip " android:ellipsize =" marquee " android:singleLine =" true " android:text =" @string/disable_profile " />
  </ LinearLayout >
  </ LinearLayout >
3.PreferenceActivity使用layout文件和Preference文件

public class CustomProfile extends PreferenceActivity{
   private BatteryProfileUtils mBatteryProfileUtils;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
      setContentView(R.layout.buttonbar);//使用布局文件
         addPreferencesFromResource(R.xml.configuration);加载配置文件
     getListView().setItemsCanFocus(true);
    Button enable=(Button)findViewById(R.id.button1);
        enable.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stu 
                }
        });
    Button disable=(Button)findViewById(R.id.button2);
        disable.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub               
                 finish();
               
            }
        });
   
    }


}

参考Android闹钟源码

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值