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鬧鐘源碼

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章