有关Theme主题
<resources>
<!-- 所有主題可以添加 .NoActionBar 让其变成无ActionBar的Activity-->
<!-- 所有使用AppCompat兼容主題前提條件如下:
1.当前Activity继承AppCompatActivity
2.添加'com.android.support:appcompat-v7:23.0.1'支持包
3.在onCreate方法中使用getSupportActionBar()方法获得当前Activity的ActionBar
注释:此主题理论上兼容所有API
-->
<!-- 主题亮色,actionbar暗色. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<!-- 主题亮色,actionbar亮色. -->
<style name="AppTheme2" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
</style>
<!-- 默认情况,所有暗色. -->
<style name="AppTheme3" parent="Theme.AppCompat">
<!-- Customize your theme here. -->
</style>
<!-- =======================================================-->
<!-- =======================================================-->
<!-- 所有使用Material主题的前提条件如下:
1.API>=21 使用才有意义
-->
<!-- 主题亮色,actionbar暗色. -->
<style name="AppTheme4" parent="android:style/Theme.Material.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<!-- 主题亮色,actionbar亮色. -->
<style name="AppTheme5" parent="android:style/Theme.Material.Light">
<!-- 自定义ActionBar的风格-->
<item name="android:actionBarStyle">@style/AppTheme5.ActionBar</item>
</style>
<!-- 自定义的ActionBar风格-->
<style name="AppTheme5.ActionBar" parent="android:style/Theme.Material.Light">
<!-- actionBar背景-->
<item name="android:background">@android:color/white</item>
<!-- actionBar阴影高度,需要API>=21才有效-->
<item name="android:elevation">@dimen/activity_horizontal_margin</item>
<!-- 显示返回键和标题-->
<item name="android:displayOptions">homeAsUp|showTitle</item>
</style>
<!-- 默认情况,所有暗色. -->
<style name="AppTheme6" parent="android:style/Theme.Material">
<!-- Customize your theme here. -->
</style>
<!--这个主题和上一个主题的区别就是有一个“@”前缀,如果有“@”前缀的表示只支持API>=21-->
<style name="AppTheme7" parent="@android:style/Theme.Material">
<!-- Customize your theme here. -->
</style>
<!-- =======================================================-->
<!-- =======================================================-->
<!--Holo主题前提条件如下:
1.API >=11
-->
<!-- 主题亮色,actionbar暗色. -->
<style name="AppTheme8" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<!-- 主题亮色,actionbar亮色. -->
<style name="AppTheme9" parent="android:Theme.Holo.Light">
<!-- Customize your theme here. -->
</style>
<!-- 默认情况,所有暗色. -->
<style name="AppThem10" parent="android:Theme.Holo">
<!-- Customize your theme here. -->
</style>
</resources>
无ActionBar透明statusBar
Android4.4以上直接引用如下主题即可
Theme.Holo.Light.NoActionBar.TranslucentDecor
android:Theme.Material.Light.NoActionBar.TranslucentDecor
有ActionBar透明statusBar
Android4.4以上添加如下主题属性即可
<item name="android:windowTranslucentStatus">true</item>
<item name="android:fitsSystemWindows">true</item>
此时StatusBar的背景颜色和当前窗口的背景颜色一样,因此,我们可以通过修改窗口背景颜色来间接达到修改状态栏StatusBar的背景颜色。
使用ToolBar来定制ActionBar以及状态栏StatusBar的背景
这小节内容,请参考我的另外一篇博客 Android5.x新特性之 Toolbar和Theme的使用
用户控制显示隐藏ActionBar
主要用于当ActionBar显示几秒之后会自动隐藏,当再次点击屏幕时ActionBar又会显示。
1.在主题中添加如下属性使得当前ActionBar可以悬浮在布局之上
<item name="android:windowActionBarOverlay">true</item>
2.在代码中控制多少秒之后ActionBar隐藏,以及点击屏幕ActionBar再次显示出来。
public class MainActivity extends Activity {
private ActionBar mActionBar;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mActionBar = getActionBar();
if (null != mActionBar) {
hideActionBar();
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
if (null != mActionBar) {
hideActionBar();
}
}
return super.dispatchTouchEvent(ev);
}
private void hideActionBar() {
mActionBar.show();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (mActionBar.isShowing()) {
mActionBar.hide();
}
}
}, 3000);
}
}