Skip to content

Instantly share code, notes, and snippets.

@miao1007
Last active October 31, 2015 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miao1007/066bd380d5bedae0ee70 to your computer and use it in GitHub Desktop.
Save miao1007/066bd380d5bedae0ee70 to your computer and use it in GitHub Desktop.
flyme 魅族 状态栏适配 translucent
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include layout="@layout/toolabr"/>
<TextView
android:fitsSystemWindows="true"
android:clipToPadding="false"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
public class FlyMeUtils {
/**
* setDarkStatusBar on FlyMe
* 设置状态栏字体为暗色 仅魅族有效
*/
public static void setDarkStatusBar(Activity activity, boolean isDark) {
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
try {
Class<?> instance = Class.forName("android.view.WindowManager$LayoutParams");
int value = instance.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON").getInt(lp);
Field field = instance.getDeclaredField("meizuFlags");
field.setAccessible(true);
int origin = field.getInt(lp);
if (isDark) {
field.set(lp, origin | value);
} else {
field.set(lp, (~value) & origin);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class LollipopUtils {
public static int getStatusBarHeight(Context context) {
Context appContext = context.getApplicationContext();
int result = 0;
int resourceId =
appContext.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = appContext.getResources().getDimensionPixelSize(resourceId);
}
Log.d("ScreenUtils", result + "");
return result;
}
public static void setStatusbarColor(Activity activity, View view) {
//对于Lollipop 的设备,只需要在style.xml中设置colorPrimaryDark即可
//对于4.4的设备,如下设置padding即可,颜色同样在style.xml中配置
Window w = activity.getWindow();
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
view.setPadding(0, getStatusBarHeight(activity), 0, 0);
}
}
}
public class MainActivity extends AppCompatActivity {
@Bind(R.id.toolbar) Toolbar toolbar;
@Bind(R.id.toolbar_holder) RelativeLayout toolbarHolder;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
setTheme(android.R.style.Theme_DeviceDefault_Light_NoActionBar);
FlyMeUtils.setDarkStatusBar(this, true);
LollipopUtils.setStatusbarColor(this, toolbarHolder);
toolbar.setTitle("Hello BugMe!");
//setTranslucentStatusBar(true);
}
}
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar_holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark">
<android.support.v7.widget.Toolbar
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"/>
</RelativeLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment