Last active
October 31, 2015 11:02
-
-
Save miao1007/066bd380d5bedae0ee70 to your computer and use it in GitHub Desktop.
flyme 魅族 状态栏适配 translucent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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