版权声明:本文为博主原创文章,未经博主允许不得转载。
获取底部虚拟导航栏的高度值
checkDeviceHasNavigationBar(Context context): 检测是否存在底部虚拟导航栏
getNavigationBarHeight(Context activity): 获取底部虚拟导航栏高度
注意事项:
1、导入类文件后需要change包名以及重新import R文件路径
2、Values目录下的文件(strings.xml、dimens.xml、colors.xml等),如果项目中存在,则复制里面的内容,不要整个覆盖
package com.why.project.navutilsdemo.utils; import android.content.Context; import android.content.res.Resources; import java.lang.reflect.Method; /** * Created by HaiyuKing * Used 底部虚拟导航栏工具类 */ public class NavUtils { private NavUtils() { throw new RuntimeException("NavUtils cannot be initialized!"); } /** * 获取底部虚拟导航栏高度 * @param activity * @return */ public static int getNavigationBarHeight(Context activity) { if (!checkDeviceHasNavigationBar(activity)) { return 0; } Resources resources = activity.getResources(); int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); //获取NavigationBar的高度 int height = resources.getDimensionPixelSize(resourceId); return height; } /** * 检测是否有底部虚拟导航栏 * @param context * @return */ public static boolean checkDeviceHasNavigationBar(Context context) { boolean hasNavigationBar = false; Resources rs = context.getResources(); int id = rs.getIdentifier("config_showNavigationBar", "bool", "android"); if (id > 0) { hasNavigationBar = rs.getBoolean(id); } try { Class systemPropertiesClass = Class.forName("android.os.SystemProperties"); Method m = systemPropertiesClass.getMethod("get", String.class); String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys"); if ("1".equals(navBarOverride)) { hasNavigationBar = false; } else if ("0".equals(navBarOverride)) { hasNavigationBar = true; } } catch (Exception e) { } return hasNavigationBar; } }
package com.why.project.navutilsdemo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; import com.why.project.navutilsdemo.utils.NavUtils; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private TextView tv_show; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_show = (TextView) findViewById(R.id.tv_show); int navigationBarHeight = NavUtils.getNavigationBarHeight(this); tv_show.setText("底部虚拟导航栏高度值为:"+navigationBarHeight + ",单位为px"); } }
无
暂时空缺
https://github.com/haiyuKing/NavUtilsDemo
原文:http://www.cnblogs.com/whycxb/p/7635745.html