以编程方式确定设备是手机还是平板电脑

Lau*_*van 3 android

我有一个包含2个布局的活动的应用程序:

  • layout/activity_main.xml - "电话模式",内部有一个视图(列表)
  • layout-w900dp/activity_main.xml - "平板电脑模式",包含2个视图(列表和详细信息)

通常,我会检查功能的详细信息:

if (findViewById(R.id.application_detail_container) != null) {
    // The detail container view will be present only in the
    // large-screen layouts (res/values-w900dp).
    // If this view is present, then the
    // activity should be in two-pane mode.
    mTwoPane = true;
}
Run Code Online (Sandbox Code Playgroud)

如何将此类方法转换为使用数据绑定?

Arp*_*tan 8

您只需在strings.xml文件中定义资源即可

在values/strings.xml中.

<bool name="is_tablet">false</bool>
Run Code Online (Sandbox Code Playgroud)

在values-w900dp/string.xml中

<bool name="is_tablet">true</bool>
Run Code Online (Sandbox Code Playgroud)

从代码中的任何位置访问此资源.这应该可以解决您的问题.

例如 :

 boolean isTablet = getResources().getBoolean(R.bool.is_tablet);
Run Code Online (Sandbox Code Playgroud)