How can we detect custom screen category in androi

2019-08-20 00:56发布

I would like to detect custom screen category in android being used in the particular activity,I would like to detect layout-sw640dp programatically,how can be this achieved.We can do the same for small,normal,large & xlarge layouts but I am not sure on this one,kindly help me on this.Thanks.

2条回答
狗以群分
2楼-- · 2019-08-20 02:02

Not sure why you need to do that. But, a quick walk-around is to use tag, for example:

In mdpi:

<?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"
  android:background="@drawable/content_bg"
  android:orientation="vertical"
  android:id="@+id/name"
  android:tag="mdpi-xxx-yyy" >

In hdpi:

  android:id="@+id/name"
  android:tag="hdpi-xxx-yyy" >

Then, read tag from your code for R.id.name.


查看更多
Summer. ? 凉城
3楼-- · 2019-08-20 02:02

sw640dp just means the smallest available dimension is at least 640dp. This is based off the available layout space for an activity, not the actual resolution of the device. So you can calculate this based off the dimensions of your root layout in the activity:

int density = getResources().getDisplayMetrics().density;
int widthDp = rootView.getWidth() / density;
int heightDp = rootView.getHeight() / density;
if(widthDp >= 640 || heightDp >= 640) {
    // qualifies for sw640dp
}
else {
    // does not qualify for sw640dp
}

In the case of the Galaxy S3, it's resolution is 1280x720, which is 640x360 converted to dp units. Since sw pertains to the available dimensions of the layout, not the resolution, I'm guessing it would not qualify for sw640dp since system decorations, such as the status bar, would reduce your layout's available height.

查看更多
登录 后发表回答