Android 沉浸式设计兼容基础类
来源:互联网 发布:seo求职简历 编辑:程序博客网 时间:2024/06/04 00:53
思路:
根据不同的版本去兼容
1、在4.4以下基本没有沉浸式设计,因为系统不支持
2、在4.4~5.0之间有的手机有虚拟导航栏,这里自己去写了一个NavigationBar,设置系统的高度
3、在5.0以上可以设置状态栏和导航栏的颜色,所以沉浸式设计主要是针对5.0以上的手机
BaseActivity
public class BaseActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); //判断版本,如果[4.4,5.0)就设置状态栏和导航栏为透明 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT && android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) { getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //设置虚拟导航栏为透明 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); } } @SuppressLint("NewApi") public void setOrChangeTranslucentColor(Toolbar toolbar, View bottomNavigationBar, int translucentPrimaryColor) { //判断版本,如果[4.4,5.0)就设置状态栏和导航栏为透明 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT && android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) { if (toolbar != null) { //1.先设置toolbar的高度 ViewGroup.LayoutParams params = toolbar.getLayoutParams(); int statusBarHeight = getStatusBarHeight(this); params.height += statusBarHeight; toolbar.setLayoutParams(params); //2.设置paddingTop,以达到状态栏不遮挡toolbar的内容。 toolbar.setPadding( toolbar.getPaddingLeft(), toolbar.getPaddingTop() + getStatusBarHeight(this), toolbar.getPaddingRight(), toolbar.getPaddingBottom()); //设置顶部的颜色 toolbar.setBackgroundColor(translucentPrimaryColor); } if (bottomNavigationBar != null) { //解决低版本4.4+的虚拟导航栏的 if (hasNavigationBarShow(getWindowManager())) { ViewGroup.LayoutParams p = bottomNavigationBar.getLayoutParams(); p.height += getNavigationBarHeight(this); bottomNavigationBar.setLayoutParams(p); //设置底部导航栏的颜色 bottomNavigationBar.setBackgroundColor(translucentPrimaryColor); } } } else if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { getWindow().setNavigationBarColor(translucentPrimaryColor); getWindow().setStatusBarColor(translucentPrimaryColor); } else { //<4.4的,不做处理 } } private int getNavigationBarHeight(Context context) { return getSystemComponentDimen(context, "navigation_bar_height"); } /** * 获取状态栏的高度 * * @param context * @return */ private int getStatusBarHeight(Context context) { // 反射手机运行的类:android.R.dimen.status_bar_height. return getSystemComponentDimen(this, "status_bar_height"); } private static int getSystemComponentDimen(Context context, String dimenName) { // 反射手机运行的类:android.R.dimen.status_bar_height. int statusHeight = -1; try { Class<?> clazz = Class.forName("com.android.internal.R$dimen"); Object object = clazz.newInstance(); String heightStr = clazz.getField(dimenName).get(object).toString(); int height = Integer.parseInt(heightStr); //dp--->px statusHeight = context.getResources().getDimensionPixelSize(height); } catch (Exception e) { e.printStackTrace(); } return statusHeight; } private static boolean hasNavigationBarShow(WindowManager wm) { Display display = wm.getDefaultDisplay(); DisplayMetrics outMetrics = new DisplayMetrics(); //获取整个屏幕的高度 display.getRealMetrics(outMetrics); int heightPixels = outMetrics.heightPixels; int widthPixels = outMetrics.widthPixels; //获取内容展示部分的高度 outMetrics = new DisplayMetrics(); display.getMetrics(outMetrics); int heightPixels2 = outMetrics.heightPixels; int widthPixels2 = outMetrics.widthPixels; int w = widthPixels - widthPixels2; int h = heightPixels - heightPixels2; return w > 0 || h > 0;//竖屏和横屏两种情况。 }}
使用:
MainActivity
public class MainActivity extends BaseActivity { private Toolbar toolbar; private View navgationbar; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar = (Toolbar) findViewById(R.id.toolbar); navgationbar = findViewById(R.id.navgationbar); setOrChangeTranslucentColor(toolbar, navgationbar, getResources().getColor(R.color.colorPrimaryDark)); }}activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="?attr/colorPrimary" android:orientation="vertical"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" app:title="我是标题" app:titleTextColor="@android:color/holo_red_light" /> <ScrollView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#fff" android:fillViewport="true"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="140dp" android:text="button" /> <Button android:layout_width="match_parent" android:layout_height="140dp" android:text="button" /> <Button android:layout_width="match_parent" android:layout_height="140dp" android:text="button" /> <Button android:layout_width="match_parent" android:layout_height="140dp" android:text="button" /> <Button android:layout_width="match_parent" android:layout_height="140dp" android:text="button" /> <Button android:layout_width="match_parent" android:layout_height="140dp" android:text="button" /> <Button android:layout_width="match_parent" android:layout_height="140dp" android:text="button" /> </LinearLayout> </ScrollView> <View android:id="@+id/navgationbar" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="0" android:background="?attr/colorPrimary" /></LinearLayout>
这里只是简单的封装,你也可以把view在封装一下
阅读全文
0 0
- Android 沉浸式设计兼容基础类
- (三十五)沉浸式设计以及兼容
- Android沉浸式状态栏设计
- android状态栏一体化、沉浸式状态栏(兼容低版本)
- android状态栏一体化、沉浸式状态栏(兼容低版本)
- android状态栏一体化、沉浸式状态栏(兼容低版本)
- Android沉浸式状态栏兼容4.4手机的实现
- android状态栏一体化、沉浸式状态栏(兼容低版本)
- android状态栏一体化、沉浸式状态栏(兼容低版本)
- android沉浸式状态栏兼容4.4-5.0均有效
- 沉浸式状态栏上下兼容
- Android 沉浸式 完全沉浸
- MaterialDesign-沉浸式设计
- 沉浸式设计
- ToolBar沉浸式状态栏的兼容实现
- 如何动态兼容沉浸式状态栏模式
- Android 沉浸式状态栏设计以及ContentView获取
- 一步两步带你实现Android沉浸式设计
- 论.gitignore文件
- Bootstrap基础11——缩略图thumbnail
- maven的动态web项目从搭建到运行完整示例(dark亲测可用)
- 使用MIGTIME分析导出导入时间
- scala断言专栏--标记你的测试
- Android 沉浸式设计兼容基础类
- RandomAccessFile类的使用介绍
- 2的(幂)n次方实现方式比较
- JAVA内存区域之堆
- Andorid so加载流程图(4.4.4_r1)
- CSU 1775: 悲催的移寝 <贪心>
- nyoj 题目 心急的c小加 贪心算法
- 【css3】animation
- 如何快速转载CSDN中的博客