天天看點

android4.0 系統Setting中bluetoothUI定制修改

由于分辨率的原因,android4.0預設的存在的UI分辨率是不能滿足我們需求的。這就會有一種需求,按照我們螢幕的分辨率修改UI的定制顯示。

這裡首先聲明bluetoothUI使用Preference來動态加載所有的bluetooh項,如果對Preference不是很熟,可以先簡單了解一下。

附上我當時學習用的而一個preference的文章,個人認為還不錯。

http://blog.csdn.net/plussoft/article/details/9993759

http://blog.csdn.net/chenzheng_java/article/details/6285966

接下來繼續我們的學習:首先我們先看一下谷歌原生的bluetoothUI如下圖:

android4.0 系統Setting中bluetoothUI定制修改

這個和我們對比比較明顯的就是分辨率越低的display,它顯示actionbar所占的螢幕大小會越大。這回嚴重影響我們的操作。

是以我們要做的第一條就是要把actionbar移除掉。(actionbar的移除是在themes.xml檔案中奧)

然後開始把具體的代碼部分在packages/app/Settings/src/com/android/bluetooth/BluetoothSettings.java

 @Override

    void addPreferencesForActivity() {

        addPreferencesFromResource(R.xml.bluetooth_settings);

        Activity activity = getActivity();

        if(activity.getActionBar() != null){

       Switch actionBarSwitch = new Switch(activity);

       if (activity instanceof PreferenceActivity) {

           PreferenceActivity preferenceActivity = (PreferenceActivity) activity;

           if (preferenceActivity.onIsHidingHeaders() || !preferenceActivity.onIsMultiPane()) {

               final int padding = activity.getResources().getDimensionPixelSize(

                       R.dimen.action_bar_switch_padding);

               actionBarSwitch.setPadding(0, 0, padding, 0);

               activity.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,

                       ActionBar.DISPLAY_SHOW_CUSTOM);

               activity.getActionBar().setCustomView(actionBarSwitch, new ActionBar.LayoutParams(

                       ActionBar.LayoutParams.WRAP_CONTENT,

                       ActionBar.LayoutParams.WRAP_CONTENT,

                       Gravity.CENTER_VERTICAL | Gravity.RIGHT));

           }

       }

       mBluetoothEnabler = new BluetoothEnabler(activity, actionBarSwitch);

        }

        setHasOptionsMenu(true);

    }

把actionbar拿掉後,bluetoothsettings.java是編譯不過的,因為藍牙的switch開關是放在actionbar上的,是以把action拿掉後需要給上面加一個判斷就可以了。

但是這樣就會存在下一個問題,就是沒有藍牙的開關了,那接下來我們要做的就是重新加一個新的開關但不在actionbar上。

代碼如下:

else

{

checkboxpreference = (CheckBoxPreference) findPreference("checkbox_add"); 

mBluetoothEnabler = new BluetoothEnabler(activity,checkboxpreference);

}

這個else是接在上一段代碼和if是并列的奧。

這裡面有三個地方需要注意。

1.我已經修改完bluetooth_settings.xml。谷歌預設的bluetooth_settings.xml實際并沒有任何preference或者控件。是以我把checkboxpreference加上,代碼如下:

 android:key= "checkbox_screen" >

    <CheckBoxPreference

android:key="checkbox_add"

android:summaryOff="Turn off Bluetooth"

android:summaryOn="Turn on Bluetooth"

android:title="bluetooth"

></CheckBoxPreference>

添加如上的xml就會在跳轉大bluetooth界面時出現打開和關閉的checkboxpreference的畫面。(如果你對checkboxpreference和checkbox分不清,可以百度一下奧)

2.你加上這個視窗開關了,發現你不管怎麼點,bluetooth并沒有進行開關的操作。對的,因為你并沒有進行checkboxPreference和bluetooth enble的關聯。

在BluetoothEnabler.java是需要重寫方法的,這個我們放到後面在去詳細講解。

3.第三個地方就是setHasOptionsMenu(true);這一句代碼。這個是使能menubutton是否有效的,我們的search for device、rename、timeout time和show reviced files這四條功能都是放到menu裡去做的奧。

既然這裡已經改完,接下來要改的就是可見性的UI了。

待續

繼續閱讀