gpt4 book ai didi

android - 半透明状态栏Android 4.4有偏移

转载 作者:行者123 更新时间:2023-11-29 20:11:29 25 4
gpt4 key购买 nike

我在 Android API 级别 19 上运行这段代码

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
final Window window = getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}

状态栏是半透明的,但它在顶部有一个奇怪的偏移。它在 Android 5 上完美运行。

这是布局的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<FrameLayout
android:id="@+id/statusbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:background="?attr/colorPrimary">
<android.support.v7.widget.Toolbar
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/toolbar"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp" />
</FrameLayout>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:id="@+id/content_container" />
</LinearLayout>

And here is the screenshot of the view

最佳答案

是的,我已经试过了,但没用。

使用这个库: https://github.com/jgilfelt/SystemBarTint

教程如下:

http://blog.raffaeu.com/archive/2015/04/11/android-and-the-transparent-status-bar.aspx

代码:

SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setNavigationBarTintEnabled(true);
tintManager.setTintColor(Color.parseColor("#20000000"));

你也可以尝试用这个方法找到状态栏的高度:

// A method to find height of the status bar
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}

我这样做了并且工作正常:

Create an XML file in your drawable folder(for exmaple: kitkat_status_bar) and put these codes on it:

<?xml version="1.0" encoding="utf-8"?>
<layer-list>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/ColorPrimary"/>
</shape>
</item>
</layer-list>

然后您可以将这些代码用于您的Activity:

if (Build.VERSION.SDK_INT < 19) {
AppUtilsSetting.setTitleBarTint(this);
}

这是类(class):

public static class AppUtilsSetting {

public static void setTitleBarTint(Activity ac) {

ac.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
SystemBarTintManager tintManager = new SystemBarTintManager(ac);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintDrawable(ac.getResources().getDrawable(R.drawable.kitkat_status_bar));
}
}

关于android - 半透明状态栏Android 4.4有偏移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34787876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com