关于实现android状态栏沉浸的几种方法

2018/06/15 10:14
阅读数 21

方法一

转载自:https://blog.csdn.net/zephyr_g/article/details/53489320

步骤一:

首先,我们先在values下的style中加入如下代码:

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.Main" parent="AppTheme" />

AppTheme.Main是我们要设置给activity的主题,它应该继承于AppTheme.NoActionBar,然后 
我们在values-v19中加入同样的AppTheme.Main,如下所示:

<style name="AppTheme.Main" parent="AppTheme.NoActionBar">
        <item name="android:windowTranslucentStatus">true</item>
    </style>

步骤二

在布局文件的根布局中加入android:fitsSystemWindows=”true” 

方法二

转载自:https://www.jianshu.com/p/b7bacc6805c2

步骤一

values

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar"/>
</resources>

values-v19

<resources>

    <!-- Base application theme. -->
    <style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
    </style>
</resources>

values-v21

<resources>

    <!-- Base application theme. -->
    <style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowTranslucentStatus">false</item>
        <item name="android:windowTranslucentNavigation">true</item>
        <!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色-->
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>

</resources>

步骤二:

在activity中添加

private android.support.v7.widget.Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar=findViewById(R.id.toolbar);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setHeight(mToolbar);
}
}

public void setHeight(View view) {
// 获取actionbar的高度
TypedArray actionbarSizeTypedArray = obtainStyledAttributes(new int[]{
android.R.attr.actionBarSize
});
float height = actionbarSizeTypedArray.getDimension(0, 0);
// ToolBar的top值
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
double statusBarHeight = getStatusBarHeight(this);
lp.height = (int) (statusBarHeight + height);
view.setPadding(0,(int) statusBarHeight,0, 0);
mToolbar.setLayoutParams(lp);
}

private double getStatusBarHeight(Context context) {
int result = 0;
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen",
"android");
if (resourceId > 0) {
result = context.getResources().getDimensionPixelSize(resourceId);
}
return result;
}

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部