1.style文件中 去Title 状态栏透明
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
...
<item name="windowNoTitle">true</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
2.通过padding撑出来状态栏
<android.support.v7.widget.Toolbar
android:id="@+id/toobar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
...
android:fitsSystemWindows="true"
android:paddingTop="@dimen/toolbar_padding_top"/>
3.在values, values-v19两个文件夹内dimens.xml下分别声明toolbar_padding_top变量,values文件夹下的值为0dp,而values-v19文件夹下的值为25dp.
<resources>
<dimen name="toolbar_padding_top">25dp</dimen>
</resources>
4.代码中设置
public class MainActivity extends AppCompatActivity {
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 经测试在代码里直接声明透明状态栏更有效
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
}
initToolbar();
}
private void initToolbar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.tb);
toolbar.setTitle("LynnRachel");
if (toolbar != null) {
setSupportActionBar(toolbar);
}
}
}