PreferenceActivity中使用layout 布局文件

来源:互联网 发布:哪个虚拟机装mac好 编辑:程序博客网 时间:2024/06/05 09:06
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闹钟源码

 

原创粉丝点击