我知道这是旧线程,但是这种方式呢:
public boolean isLargeScreen() {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.device_screen, null);
FrameLayout menu = (FrameLayout) view.findViewById(R.id.menu);
if (left_menu != null) {
return true;
} else {
return false;
}
}
然后:
if ( isLargeScreen() ) {
// do something for large screen & extra large screen
} else {
// do something for normal screen size
}
和 xml 布局(例如):
res/layout/device_screen.xml // 正常屏幕尺寸的布局(“默认”)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
[leave it blank]
</LinearLayout>
res/layout-large/device_screen.xml // 大屏幕尺寸 tablet 7' 的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/menu"
... />
</LinearLayout>
res/layout-xlarge/device_screen.xml // 超大屏幕尺寸 > 平板电脑 10' 的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/menu"
... />
</LinearLayout>
在 HTC Desire 和三星 Galaxy Note 10.1 上测试 - 效果很好!