【发布时间】:2016-03-09 11:53:07
【问题描述】:
我正在努力让我的布局被 Android 定制和采用。
我使用以下资源目录创建了一个测试应用程序:
- 布局
- 布局大
- 布局正常
- 小布局
- layout-sw600dp
- 布局-sw720dp
- 布局-w600dp
- 布局-w720dp
- layout-xlarge
它们基本上都包含相同的布局文件,唯一的区别是一个文本视图只是具有布局目录名称的内容:
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnTest"
android:text="Layout xxxxx" />
我的问题是,无论我创建什么 AVD,它们都会拾取 Normal 布局文件。 这是我测试过的 AVD,以及此调试代码的输出:
Configuration config = getResources().getConfiguration();
String sizeName = getSizeName(this);
Log.d(TAG, String.format("screen config smallestScreenWidthDp:%d screenWidthDp:%d densityDpi:%d sizeName: %s" ,
config.smallestScreenWidthDp, config.screenWidthDp, config.densityDpi, sizeName)); //NON-NLS
- Nexus 6P,1440x2560,518ppi:
- 最小屏幕宽度Dp:360 screenWidthDp:360 densityDpi:640 sizeName: normal
- 银河 S4 迷你,
4.3" 540x960, 256ppi:
- smallestScreenWidthDp:360 screenWidthDp:360 densityDpi:240 sizeName: normal
- 维科果阿,3.5"
320x480、165 ppi:
- 最小屏幕宽度Dp:320 screenWidthDp:320 densityDpi:160 sizeName: normal
我做错了什么?
编辑:这不是 AVD 问题,我在物理设备上得到相同的行为;我的应用在 Nexus 6P(1440x2560, 518ppi) 和 Wiko GOA(320x480, 165ppi) 设备上都选择了 Normal 布局。
这是我的test project 供参考。
【问题讨论】:
-
请试试这个 DisplayMetrics metrics = getResources().getDisplayMetrics();
-
将您的大文件夹、普通文件夹和小文件夹命名为 sw
符号。不要将两种命名约定混在一起 -
@M.S.P ,那我该怎么办? DisplayMetrics.densityDpi 返回的值与我的其他日志输出相同。
-
@VivekMishra,删除 sw
和 w 文件夹,只保留 -small、-normal、-large 和 -xlarge 不变,什么也没做。 -
小布局现在不起作用
标签: android screen-size