This subject is discussed in the Android Training:
Use the Smallest-width Qualifier
If you read the entire topic, they explain how to set a boolean value in a specific value file (as res/values-sw600dp/attrs.xml):
<resources>
<bool name="isTablet">true</bool>
</resources>
Because the sw600dp qualifier is only valid for platforms above android 3.2. If you want to make sure this technique works on all platforms (before 3.2), create the same file in res/values-xlarge folder:
<resources>
<bool name="isTablet">true</bool>
</resources>
Then, in the "standard" value file (as res/values/attrs.xml), you set the boolean to false:
<resources>
<bool name="isTablet">false</bool>
</resources>
Then in you activity, you can get this value and check if you are running in a tablet size device:
boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
if (tabletSize) {
// do something
} else {
// do something else
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…