Joh*_*ohn 118
如前所述,您不想检查设备是平板电脑还是手机,但您想了解设备的功能,
大多数情况下,平板电脑和手机之间的区别在于屏幕尺寸,这就是您要使用不同布局文件的原因.这些文件存储在res/layout-<qualifiers>目录中.您可以在res/values-<same qualifiers>每个布局的directoy中创建一个XML文件,并将int/bool/string资源放入其中,以区分您使用的布局.
文件res/values/screen.xml(假设res/layout/包含手机的布局文件)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="screen_type">phone</string>
</resources>
Run Code Online (Sandbox Code Playgroud)
文件res/values-sw600dp/screen.xml(假设res/layout-sw600dp/包含Nexus 7等小型平板电脑的布局文件)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="screen_type">7-inch-tablet</string>
</resources>
Run Code Online (Sandbox Code Playgroud)
文件res/values-sw720dp/screen.xml(假设res/layout-sw720dp/包含Nexus 10等大型平板电脑的布局文件):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="screen_type">10-inch-tablet</string>
</resources>
Run Code Online (Sandbox Code Playgroud)
现在可以通过R.string.screen_type常量访问屏幕类型.
pet*_*tey 60
要检测设备是否为平板电脑,请使用以下代码:
public boolean isTablet(Context context) {
boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE);
boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
return (xlarge || large);
}
Run Code Online (Sandbox Code Playgroud)
LARGE和XLARGE屏幕尺寸由制造商根据他们将要使用的眼睛的距离来确定(因此是平板电脑的想法).
更多信息:http://groups.google.com/group/android-developers/browse_thread/thread/d6323d81f226f93f
Hel*_*sac 38
这篇文章给了我很多帮助,
不幸的是,我没有必要的声誉来评估帮助我的所有答案.
我需要确定我的设备是平板电脑还是手机,我能够实现屏幕逻辑.在我的分析中,平板电脑必须从MDPI开始超过7英寸(Xlarge).
这是下面的代码,它是根据这篇文章创建的.
/**
* Checks if the device is a tablet or a phone
*
* @param activityContext
* The Activity Context.
* @return Returns true if the device is a Tablet
*/
public static boolean isTabletDevice(Context activityContext) {
// Verifies if the Generalized Size of the device is XLARGE to be
// considered a Tablet
boolean xlarge = ((activityContext.getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK) ==
Configuration.SCREENLAYOUT_SIZE_XLARGE);
// If XLarge, checks if the Generalized Density is at least MDPI
// (160dpi)
if (xlarge) {
DisplayMetrics metrics = new DisplayMetrics();
Activity activity = (Activity) activityContext;
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
// MDPI=160, DEFAULT=160, DENSITY_HIGH=240, DENSITY_MEDIUM=160,
// DENSITY_TV=213, DENSITY_XHIGH=320
if (metrics.densityDpi == DisplayMetrics.DENSITY_DEFAULT
|| metrics.densityDpi == DisplayMetrics.DENSITY_HIGH
|| metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM
|| metrics.densityDpi == DisplayMetrics.DENSITY_TV
|| metrics.densityDpi == DisplayMetrics.DENSITY_XHIGH) {
// Yes, this is a tablet!
return true;
}
}
// No, this is not a tablet!
return false;
}
Run Code Online (Sandbox Code Playgroud)
Vol*_*ing 12
为什么不计算屏幕对角线的大小,并用它来决定设备是手机还是平板电脑?
private boolean isTablet()
{
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics displayMetrics = new DisplayMetrics();
display.getMetrics(displayMetrics);
int width = displayMetrics.widthPixels / displayMetrics.densityDpi;
int height = displayMetrics.heightPixels / displayMetrics.densityDpi;
double screenDiagonal = Math.sqrt( width * width + height * height );
return (screenDiagonal >= 9.0 );
}
Run Code Online (Sandbox Code Playgroud)
当然可以争论阈值是否应该是9英寸或更小.
Nan*_*nne 11
没有区别.你应该定义你认为不同的东西,然后检查一下.星系标签是手机吗?还是平板电脑?为什么?
您应该定义要查找的特定功能,并为其编写代码.
看来你正在寻找'倾斜'.我认为这跟加速度计一样(是一个字吗?).您可以使用以下方法检查设备是否支持它:
public class Accel extends Activity implements SensorListener {
...
SensorManager sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
boolean accelSupported = sensorMgr.registerListener(this,
SENSOR_ACCELEROMETER,
SENSOR_DELAY_UI);
...
}
Run Code Online (Sandbox Code Playgroud)
(来自http://stuffthathappens.com/blog/2009/03/15/android-accelerometer/.我还没有测试过)
Rob*_*III 10
我的假设是,当您定义"移动/电话"时,您希望知道您是否可以在设备上拨打电话,而这些电话无法用于定义为"平板电脑"的内容.验证这一点的方法如下.如果你想知道基于传感器,屏幕尺寸等的东西,那么这实际上是一个不同的问题.
此外,虽然使用屏幕分辨率,或资源管理大与xlarge,可能是一个有效的方法在过去新的'移动'设备现在有这么大的屏幕和高分辨率,他们正在模糊这一行,如果你真的希望要知道电话呼叫与没有电话呼叫功能,以下是"最佳".
TelephonyManager manager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if(manager.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE){
return "Tablet";
}else{
return "Mobile";
}
Run Code Online (Sandbox Code Playgroud)
在Google IOSched 2017 app源码中,使用了如下方法:
public static boolean isTablet(Context context) {
return context.getResources().getConfiguration().smallestScreenWidthDp >= 600;
}
Run Code Online (Sandbox Code Playgroud)
基于罗伯特戴尔约翰逊三世和赫尔顿伊萨克我想出了这个代码希望这是有用的
public static boolean isTablet(Context context) {
TelephonyManager manager =
(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if (manager.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE) {
//Tablet
return true;
} else {
//Mobile
return false;
}
}
public static boolean isTabletDevice(Context activityContext) {
// Verifies if the Generalized Size of the device is XLARGE to be
// considered a Tablet
boolean xlarge =
((activityContext.getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE);
// If XLarge, checks if the Generalized Density is at least MDPI (160dpi)
if (xlarge) {
DisplayMetrics metrics = new DisplayMetrics();
Activity activity = (Activity) activityContext;
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
// MDPI=160, DEFAULT=160, DENSITY_HIGH=240, DENSITY_MEDIUM=160,
// DENSITY_TV=213, DENSITY_XHIGH=320
if (metrics.densityDpi == DisplayMetrics.DENSITY_DEFAULT
|| metrics.densityDpi == DisplayMetrics.DENSITY_HIGH
|| metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM
|| metrics.densityDpi == DisplayMetrics.DENSITY_XHIGH) {
// Yes, this is a tablet!
return true;
}
}
// No, this is not a tablet!
return false;
}
Run Code Online (Sandbox Code Playgroud)
所以在你的代码中制作一个过滤器
if(isTabletDevice(Utilities.this) && isTablet(Utilities.this)){
//Tablet
} else {
//Phone
}
Run Code Online (Sandbox Code Playgroud)
其他答案列出了以编程方式确定设备是手机还是平板电脑的多种方法。但是,如果您阅读文档,就会发现这不是支持各种屏幕尺寸的推荐方法。
相反,为平板电脑或手机声明不同的资源。layout您可以通过为、values等添加其他资源文件夹来执行此操作。
对于 Android 3.2(API 级别 13),添加一个sw600dp文件夹。这意味着最小宽度至少为 600dp,大约是手机/平板电脑的宽度。但是,您也可以添加其他尺寸。查看此答案,了解如何添加附加布局资源文件的示例。
如果您还支持 Android 3.2 之前的设备,那么您将需要添加large或xlarge文件夹来支持平板电脑。(电话一般是small和normal。)
下面是针对不同屏幕尺寸添加额外 xml 文件后您的资源可能会喜欢的图像。
使用此方法时,系统会为您决定一切。您不必担心运行时正在使用哪个设备。您只需提供适当的资源,然后让 Android 完成所有工作。
笔记
| 归档时间: |
|
| 查看次数: |
80504 次 |
| 最近记录: |