在java代碼中:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
<span style="color:#FF0000;">requestWindowFeature(Window.FEATURE_NO_TITLE);//設置沒有titlebar</span>
setContentView(R.layout.activity_main);
set();
initView();
}
private void set() {
<span style="color:#FF0000;">if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
// 透明狀態欄
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// 透明導航欄
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}</span>
}
private void initView() {
}
}
直接調用上面2行代碼可以透明,但是你會發現你的 view 跑到 actionbar 上面去了,很明顯 google 的意圖是使你的 view 可以佔據整個屏幕,然後 狀態欄和導航欄 透明覆蓋在上面很明顯這樣不可行。
那有沒有辦法使你的 view 保持原來大小呢?
有,你需要在這個 activity 的 layout xml 文件添加兩個屬性
在xml中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
<span style="color:#FF0000;"> android:clipToPadding="true"
android:fitsSystemWindows="true"</span>
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="15dp"
android:paddingTop="15dp"
android:text="@string/hello_world"
android:textColor="#FFFFFF"
android:textSize="18sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" />
</LinearLayout>
結果:
4.4之前的設備上效果
4.4之後的設備上效果
另一種方式是使用系統自帶的titlebar,這樣的話用上面的就不是那麼方便了,推薦下面的帖子
http://segmentfault.com/a/1190000000403651
1. 設置主題
<style name="Theme.Timetodo" parent="@android:style/Theme.Holo.Light">
<!-- translucent system bars -->
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
添加了這兩個屬性之後 就是是這個效果了 可以看到 listview已經被頂到上面去了 不知道是不是bug 查了下資料 目前的解決辦法好像都是給layout設置padding來解決
2.設置顏色和設置padding
private void initSystemBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintResource(R.color.actionbar_bg);
SystemBarConfig config = tintManager.getConfig();
listViewDrawer.setPadding(0, config.getPixelInsetTop(true), 0, config.getPixelInsetBottom());
}
}
3.最終效果
參考資料:
http://stackoverflow.com/questions/20781014/translucent-system-bars-and-content-margin-in-kitkat
if (android.os.Build.VERSION.SDK_INT > 18) {
Window window = getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
// 創建狀態欄的管理實例
SystemBarTintManager tintManager = new SystemBarTintManager(this);
// 激活狀態欄設置
tintManager.setStatusBarTintEnabled(true);
// 激活導航欄設置
tintManager.setNavigationBarTintEnabled(true);
// 設置一個顏色給系統欄
tintManager.setTintColor(Color.parseColor("#FFFF6666"));
}