我想了解一种设备的信息,看看它是智能手机还是平板电脑。我该怎么做呢?
我想显示不同的网页资源基于设备的类型:
String s="Debug-infos:";
s += "\n OS Version: " + System.getProperty("os.version") + "(" + android.os.Build.VERSION.INCREMENTAL + ")";
s += "\n OS API Level: " + android.os.Build.VERSION.SDK;
s += "\n Device: " + android.os.Build.DEVICE;
s += "\n Model (and Product): " + android.os.Build.MODEL + " ("+ android.os.Build.PRODUCT + ")";
然而,这似乎对我的情况毫无用处。
这个解决方案现在对我很有效:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
if (SharedCode.width > 1023 || SharedCode.height > 1023){
//code for big screen (like tablet)
}else{
//code for small screen (like smartphone)
}
我发现最好的选择和侵入性较小,是在xml中设置一个标记参数,如
电话XML布局
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="phone"/>
Tablet XML布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="tablet">
...
</RelativeLayout>
然后在你的活动类中调用这个
View viewPager = findViewById(R.id.pager);
Log.d(getClass().getSimpleName(), String.valueOf(viewPager.getTag()));
希望这对你有用。
我发现最好的选择和侵入性较小,是在xml中设置一个标记参数,如
电话XML布局
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="phone"/>
Tablet XML布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="tablet">
...
</RelativeLayout>
然后在你的活动类中调用这个
View viewPager = findViewById(R.id.pager);
Log.d(getClass().getSimpleName(), String.valueOf(viewPager.getTag()));
希望这对你有用。
I like Ol_v_er's solution and it's simplicity however, I've found that it's not always that simple, what with new devices and displays constantly coming out, and I want to be a little more "granular" in trying to figure out the actual screen size. One other solution that I found here by John uses a String resource, instead of a boolean, to specify the tablet size. So, instead of just putting true in a /res/values-sw600dp/screen.xml file (assuming this is where your layouts are for small tablets) you would put:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="screen_type">7-inch-tablet</string>
</resources>
参考如下,然后根据结果做你需要做的事情:
String screenType = getResources().getString(R.string.screen_type);
if (screenType.equals("7-inch-tablet")) {
// do something
} else {
// do something else
}
Sean O'Toole对检测7英寸和10英寸平板电脑的回答也是我一直在寻找的。如果这里的答案不允许你像你想的那样具体,你可能会想要检查一下。他很好地解释了如何计算不同的度量标准,以弄清楚你实际在处理什么。
更新
在查看谷歌I/O 2013应用程序源代码时,我看到了他们用来识别设备是否是平板电脑的以下内容,所以我想我应该添加它。以上内容让你对它有了更多的“控制”,但如果你只是想知道它是否是平板电脑,下面的步骤相当简单:
public static boolean isTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK)
>= Configuration.SCREENLAYOUT_SIZE_LARGE;
}