<style name="MyAppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:windowTranslucentStatus">true</item> <item name="android:windowTranslucentNavigation">false</item> </style>然后在Mainfest文件中,将MainActivity的theme设置成MyAppTheme,即可实现效果。2.activity_main.xml中将背景设置成蓝色
<?xml%20version="1.0"%20encoding="utf-8"?><RelativeLayout%20xmlns:android="http://schemas.android.com/apk/res/android"%20%20%20%20xmlns:tools="http://schemas.android.com/tools"%20%20%20%20android:id="@+id/activity_main"%20%20%20%20android:layout_width="match_parent"%20%20%20%20android:layout_height="match_parent"%20%20%20%20android:background="@android:color/holo_blue_light"%20%20%20%20tools:context="karry.example.com.testtheme.MainActivity"></RelativeLayout>此时透明效果就实现了。注:这种方法只使用于19及以上的版本。二、实现状态栏自定义颜色(API>=21)
1:新建values-v21文件夹,在其中新建styles.xml,新建主题MyAppTheme:
效果图:
<?xml version="1.0" encoding="utf-8"?><resources> <style name="MyAppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:colorPRimaryDark">@color/colorPrimaryDark</item> </style></resources>其中colorPrimaryDark在color.xml中值为:#ff0000
2:activity.xml<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_blue_light" tools:context="karry.example.com.testtheme.MainActivity"></RelativeLayout>三、在代码中实现以上两个步骤
API>=19&&API<21 :实现状态栏的透明API>=21 :实现状态栏的自定义颜色(在此设置为蓝色)只需要在MainActivity中:public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setStuats(); } private void setStuats() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLipOP) { Window window = getWindow(); window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);//设置顶部状态栏颜色 window.setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark)); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); } }}当然你也可以使用第三方框架:SystemBarTint
新闻热点
疑难解答