[英]support for different screen sizes in android
i want to my apps support different screen sizes. 我想我的应用程序支持不同的屏幕尺寸。 i add folders "layout-small and layout-large " in /res directory.
我在/ res目录中添加文件夹“ layout-small和layout-large”。 but XMLs inside this folders aren't accessible in my activity.so i add all my XMLs in default layout and add this code
但是无法在我的活动中访问此文件夹中的XML。因此,我将所有XML添加到默认布局中并添加此代码
if((getResources().getConfiguration().screenLayout &&
Configuration.SCREENLAYOUT_SIZE_SMALL) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
setContentView(R.layout.main1);
}else if((getResources().getConfiguration().screenLayout &&
Configuration.SCREENLAYOUT_SIZE_LARGE) == Configuration.SCREENLAYOUT_SIZE_LARGE){
setContentView(R.layout.main2);
}
else
setContentView(R.layout.main);
in my activity, but when my AVD skin is 1024*600 and hw.lcd.dencity is 160 (large) it didn't work. 在我的活动中,但是当我的AVD皮肤为1024 * 600且hw.lcd.dencity为160(大)时,它不起作用。
any help? 有什么帮助吗?
Size: small, normal, large
尺寸:小,普通,大
Density: ldpi, mdpi, hdpi,密度:ldpi,mdpi,hdpi,
nodpi(no auto-scale) Aspect ratio: long, notlongnodpi(无自动缩放)长宽比:长,不长
Orientation: land方向:土地
Usage: 用法:
res/layout/my_layout.xml
res/layout-small/my_layout.xml
res/layout-large/my_layout.xml
res/layout-large-long/my_layout.xml
res/layout-large-land/my_layout.xml
res/drawable-ldpi/my_icon.png
res/drawable-mdpi/dpi/my_icon.png
res/drawable-hdpi/my_icon.png
res/drawable-nodpi/composite.xml
Restricting your app to specific screen sizes(via the AndroidManifest): 将您的应用限制为特定的屏幕尺寸(通过AndroidManifest):
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
...
<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true" />
...
</manifest>
And for code level tweeking: 对于代码级别的tweeking:
float scale = getContext().getResources().getDisplayMetrics().density;
And don't forget: 并且不要忘记:
dpi = 160; //At 160dpi
pixels = dips * (density / dpi)
Also refer support multiple screen in android 另请参阅在Android中支持多屏
Layout name must be same 布局名称必须相同
layout-small \ main1.xml
layout-normal\ main1.xml
layout-large \ main1.xml
You dont need to handle this, android automatically decide which layout will be used 您不需要处理此问题,Android会自动决定将使用哪种布局
setContentView(R.layout.main1);
Please look at this: 请看这个:
<supports-screens android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true" />
res/layout/my_layout.xml // layout for normal screen size ("default")
res/layout-small/my_layout.xml // layout for small screen size
res/layout-large/my_layout.xml // layout for large screen size
res/layout-xlarge/my_layout.xml // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation
res/drawable-mdpi/my_icon.png // bitmap for medium density
res/drawable-hdpi/my_icon.png // bitmap for high density
res/drawable-xhdpi/my_icon.png // bitmap for extra high density
more read so please see this link How to support different screen size in android 更多阅读,所以请看这个链接如何在Android中支持不同的屏幕尺寸
Android automatically does this for you. Android会自动为您执行此操作。 For the different screen sizes, make different xml files, but give them the same name, for example
main.xml
and put the one for large
in the folder /res/layout-large
, for small
in /res/layout-small
, et cetera. 对于不同的屏幕尺寸,请制作不同的xml文件,但要给它们指定相同的名称,例如
main.xml
然后将large
文件放在/res/layout-large
文件夹中,将small
放在/res/layout-small
等文件夹中。等等
in the onCreate()
, just put setContentView(R.layout.main)
在
onCreate()
,只需将setContentView(R.layout.main)
For more information, consider reading this site from Android Developers . 有关更多信息,请考虑从Android Developers阅读本网站 。
Android will automatically pick up the best resource for a given particular device.If no matching resource is available, the system uses the default resource and scales it up or down as needed to match the current screen size and density. Android将自动为给定的特定设备选择最佳资源。如果没有可用的匹配资源,系统将使用默认资源并根据需要将其放大或缩小以匹配当前屏幕尺寸和密度。
The "default" resources are those that are not tagged with a configuration qualifier. “默认”资源是那些没有用配置限定符标记的资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.