【问题标题】:how to add address layout-small activity.xml in class如何在类中添加地址layout-small activity.xml
【发布时间】:2017-10-28 17:25:32
【问题描述】:

您好,本教程https://stackoverflow.com/q/7665194/840861make res/layout-small/my_layout.xml,我想在主要活动中添加这个xml文件,但不能像本教程https://android-developers.googleblog.com/2011/07/new-tools-for-managing-screen-sizes.html那样解决它

我想要这样的代码:

public class MyActivity extends Activity {
@Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate();

    Configuration config = getResources().getConfiguration();
    if (config.smallestScreenWidthDp >= 600) {
        setContentView(R.layout.main_activity_tablet);
    } else {
        setContentView(R.layout.main_activity);
    }
}
}

我的代码是:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(R.layout.activity_main);
}

但是不能这个地址xml:

setContentView(R.layout-small.activity_main);

我创建成功文件夹布局-小和过去的xml文件activity_main但无法添加地址

对不起,我的英语不好

【问题讨论】:

    标签: android android-layout screen-size setcontentview


    【解决方案1】:

    制作 res/layout-small/my_layout.xml

    没有理由创建res/layout-small/ 目录。这与res/layout/ 的效果相同,表示将这些资源用于每种屏幕尺寸。

    大小限定符(-small-normal-large-xlarge 以及它们更现代的等价物,如 -w640dp)说“将这些资源用于此大小或更大的任何东西”。因此,res/layout-large/ 资源不会在-small-normal 屏幕上使用,res/layout-w640dp/ 资源不会在当前宽度小于 640dp 的设备上使用。但是所有屏幕都是-small 或更大,所以res/layout-small/ 将匹配每个设备。


    setContentView(R.layout-small.activity_main);
    

    当你使用一个资源时,你永远不会把限定词放进去。把它替换为:

    setContentView(R.layout.activity_main);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      • 2011-12-30
      • 2021-09-09
      • 1970-01-01
      • 2015-09-05
      相关资源
      最近更新 更多