且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用不同的主题取决于如果设备是Android平板电脑或手机

更新时间:2023-02-09 19:55:36

您可以动态每项活动像这里面的设置

You can set it dynamically inside each activity like this:

 protected void onCreate(Bundle icicle) {

    super.onCreate(icicle);

    // ...

    // Call setTheme before creation of any(!) View.

    if(isTablet()) {
        setTheme(android.R.style.Black);
    }
    else {
        setTheme(android.R.style.Theme_Dark);
    }     
    // ...

    setContentView(R.layout.main);
}

现在你需要 isTablet 方法,但它是一个有点难以察觉的设备类型。这里有一个方法,我在网上找到,它会检查屏幕尺寸,如果屏幕大它假定当前设备是平板电脑:

Now you need isTablet method but it is a bit difficult to detect device type. Here is a method I found online, it checks the screen size and if the screen is big it assumes current device is a tablet.:

public boolean isTablet() {
    try {
        // Compute screen size
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        float screenWidth  = dm.widthPixels / dm.xdpi;
        float screenHeight = dm.heightPixels / dm.ydpi;
        double size = Math.sqrt(Math.pow(screenWidth, 2) +
                                Math.pow(screenHeight, 2));
        // Tablet devices should have a screen size greater than 6 inches
        return size >= 6;
    } catch(Throwable t) {
        Log.error(TAG_LOG, "Failed to compute screen size", t);
        return false;
    }

}