布局图如下:
1.ActionBar:Switch
在Android4.0中,在主界面Settings中定义了很多内部子空类,例如:
public static class BluetoothSettingsActivity extends Settings
在Mainifest.xml中,配置了一些对应的fragment。例如:
<activityandroid:name="Settings$BluetoothSettingsActivity"
android:label="@string/bluetooth_settings_title"
android:clearTaskOnLaunch="true">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN" />
<action android:name="android.settings.BLUETOOTH_SETTINGS"/>
<categoryandroid:name="android.intent.category.VOICE_LAUNCH" />
<category android:name="com.android.settings.SHORTCUT"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<meta-dataandroid:name="com.android.settings.FRAGMENT_CLASS"
android:value="com.android.settings.bluetooth.BluetoothSettings"/>
<meta-dataandroid:name="com.android.settings.TOP_LEVEL_HEADER_ID"
android:resource="@id/bluetooth_settings" />
</activity>
可以看到他对应的fragment是
com.android.settings.bluetooth.BluetoothSettings
所以说我们从一级界面Settings点击相应的header跳转到BT的设置的界面的时候,实际上是启动相应的Activity加上fragment。
这 里当然是BluetoothSettingsActivity加上BluetoothSettings,对于ActionBar这部分,我们可以看到是由两部分主城,其中一部分是由icon和文字来表示当前的界面,其中icon始终没有变,而文字是要相应的 变化的,而这个文字的设置就在 Mainfest中配置的: android:label="@string/bluetooth_settings_title"。 另一部分是一个Switcher来控制蓝牙的关闭。这个switch就是一个widget,他的基本关系:public class Switch extendsCompoundButton。 Switch是在BluetoothSettings代码中添加的:
activity.getActionBar().setCustomView(actionBarSwitch,new ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.WRAP_CONTENT,
Gravity.CENTER_VERTICAL | Gravity.RIGHT));
后面又将actionBarSwitch传入到BluetoothEnabler,通过BluetoothEnable