我正在开发一款需要支持手机和平板电脑的Android应用(minSdk:14)。支持我的意思是布局和图像(高度,宽度,填充,边距等)应适当缩放到不同的屏幕尺寸。我不需要平板电脑的“不同”布局,例如导航抽屉或重新布置的布局。
起初我尝试创建layout-sw600dp,layout-sw720dp ...但是我意识到我只是复制布局而只改变他们的度量(高度,宽度等)。我不喜欢这样,因为将来很难保持这种状态。
相反,我将所有布局都保留在默认/布局文件夹中。我创建了几个dimens.xml并将它们放在文件夹/值,/ values-sw600dp等中。
对于/values/dimens.xml,我定义了:
<dimen name="dp1">1dp</dimen>
<dimen name="dp2">2dp</dimen>
...
<dimen name="dp100">100dp</dimen>
对于/values-sw600dp/dimens.xml,我定义了:
<dimen name="dp1">2dp</dimen>
<dimen name="dp2">4dp</dimen>
...
<dimen name="dp100">200dp</dimen>
正如您所看到的,我为平板电脑sw600加倍了dps。
然后我更改了所有布局测量(高度,宽度,填充,边距)以使用“@dimen /..."
这样,布局似乎可以在我测试的所有设备上正确扩展。 我希望有一些专家对这次设计选举的意见。
1)这种方法有问题吗?
2)你能建议更好的方法来达到预期的效果吗?
谢谢!
答案 0 :(得分:0)
Andres,解释android布局设计以支持您需要阅读link
的多个屏幕然后还让我在这里解释一下,你需要创建Draw Drawables文件夹,如Drawable-hdpi,Drawable-mdpi,Drawable-xhdpi,Drawable-xxhdpi,并在这4个文件夹中放入相同名称的相同图像根据支持的分辨率,您的要求的大小。
然后,同样地,你需要创建多个布局文件来支持多个屏幕,剩下的所有东西都将由Android自己处理。
答案 1 :(得分:0)
使用值资源正确执行此操作。目标与android无关,但与防止冗余代码的一般编程实践和单个位置值更改以反映无处不在。希望这会有所帮助:)
答案 2 :(得分:0)
you can use dimen file and drawble image in diffrent folder so you can design for all layout.
Everything about sizes and dimensions in Android
By Android Teacher on July 6th, 2013 in Appendix tags: android:textSize, dimen, dimens.xml, dip, dp, getConfiguration, getDimension, getDimensionPixelSize, getDisplayMetrics, hdpi, in, layout-sw600dp, layout-sw720dp, ldpi, mdpi, mm, pt, px, setContentView, smallestScreenWidthDp, sp, xhdpi, xxhdpi
Designing Android app you have to be aware of enormous variety of devices. They have different physical sizes (from small phones to large tablets), different screen sizes (diagonal from 3 inches or less to above 5 inches), different screen resolutions (from 320×480 pixels to Full HD – 1920×1080 – or more) and different screen densities – number of pixels per inch (from 120 dpi to 480 dpi). And there is also screen orientation (landscape or portrait) to take into account.
List of supported units:
dp or dip – density-independent pixels, abstract unit based on physical density of a screen
in – inches (not recommended)
mm – millimeters (not recommended)
pt – points, 1pt equals 1/72 of inch (not recommended)
px – pixels (not recommended)
You have to put unit after the value without space between.
For instance:
android:layout_width="120dp"
1
android:layout_width="120dp"
Measure units comparison in Android – only dp is universal (Android Studio)
Measure units comparison in Android – only dp is universal (Android Studio)
But what is easy in XML, it’s not so easy in Android Java. Though recommended unit is dp, Java methods use px as default. You have to do conversion on your own. First you have to check device scale ratio and then multiply it by dps you need (and add 0.5 to round it up). The result is float number that have to be change to int for measure methods.
This is an example (textElement is for instance TextView):
float scaleRatio = getResources().getDisplayMetrics().density;
int dps = 100;
int pixels = (int) (dps * density + 0.5f);
textElement.setWidth(pixels);
float scaleRatio = getResources().getDisplayMetrics().density;
int dps = 100;
int pixels = (int) (dps * density + 0.5f);
textElement.setWidth(pixels);
This is rather complicated so better define layout in XML or use… dimens.xml described below.
For text size use scale-independent pixels (sp)
When you define font size you could use all previously mentioned measure units. But there is one more unit created especially for text. It’s called scale-independent pixels (abbreviation “sp”). It’s very similar to dp, but have one more characteristic – reacts to user preferences about text size. In most cases sp equals dp, but if user want to scale text it would allow to increase/decrease font without changing the size of other elements. So it’s recommended to use sp every time you refer to fonts.
<resources>
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="button_height">48dp</dimen>
<dimen name="textview_length">100dp</dimen>
<dimen name="title_size">32sp</dimen>
</resources>
<resources>
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="button_height">48dp</dimen>
<dimen name="textview_length">100dp</dimen>
<dimen name="title_size">32sp</dimen>
</resources>
You could refer to them both in XML and in Java.
XML example:
android:layout_width="@dimen/textview_length"
1
android:layout_width="@dimen/textview_length"
Java example:
textElement.setWidth(getResources().getDimensionPixelSize(R.dimen.textview_length));
1
textElement.setWidth(getResources().getDimensionPixelSize(R.dimen.textview_length));
As you see in Java you have to refer to resources and use getDimensionPixelSize() method (that converts result to int, there is also getDimension() method that returns float value without rounding). Be careful as setWidth(R.dimen.textview_length) doesn’t show any error, but only because resources has int IDs. In this case you use ID value not value saved in dimens.xml.
There is one very useful effect of using getDimensionPixelSize() or getDimension(). They automatically convert dp or sp into pixels based on screen density. So you don’t have to do it on your own. If your text size is 16sp on mdpi screen it would have 16px, but on hdpi screen it would have 24 px.
Use many dimens.xml for various device types
Using dp and sp we have guaranteed ourselves that our layout would look the same on screens with different density. But usually we have much more space on 10 inch tablet than 4 inch phone. In the first case layout elements could be bigger.
To use various dimensions for different devices we could create separate dimens.xml files for them. Or to be more precise Android Studio already created them, so our aim is just to fill them with right values.
In project structure you could find values folders for smartphones, medium tablets and big tablets (Android Studio)
In project structure you could find values folders for smartphones, medium tablets and big tablets (Android Studio)
There are three values folders interesting for as: values, values-sw600dp and values-sw720dp-land. First one is mainly for smartphones, second for 7 inch tablets and third for 10 inch tablets (in landscape orientation). You could use just two of them: first for phone and second for all tablets.
In all of them you could find dimens.xml files (if not, just create them). This allows you to decide what size of objects and text would be on different devices.
For instance:
<!-- phones -->
<dimen name="title_size">24sp</dimen>
<dimen name="button_width">64dp</dimen>
<!-- phones -->
<dimen name="title_size">24sp</dimen>
<dimen name="button_width">64dp</dimen>
<!-- small tablets -->
<dimen name="title_size">32sp</dimen>
<dimen name="button_width">80dp</dimen>
<!-- small tablets -->
<dimen name="title_size">32sp</dimen>
<dimen name="button_width">80dp</dimen>
<!-- big tablets -->
<dimen name="title_size">48sp</dimen>
<dimen name="button_width">128dp</dimen>
1
2
3
<!-- big tablets -->
<dimen name="title_size">48sp</dimen>
<dimen name="button_width">128dp</dimen>
Dedicated layout files for various device types (and screen buckets concept)
Android creators introduced so called buckets to divide devices based on their screens parameters. There were defined four size buckets:
small – screens at least 426dp x 320dp (all smaller are scaled to it)
normal – screens at least 470dp x 320dp
large – screens at least 640dp x 480dp
xlarge – screens at least 960dp x 720dp.
If you want to address large device, you could create folder layout-large and keep there a copy of your layout files.
Unfortunately that concept didn’t work to good as in the large group you could have phones and small tablets that usually need different approach.
New concept is based on minimum available width in dp (for instance in case of 640dp x 480dp it equals 480 dp). There are now three standard selectors and related layout folders:
layout – for all devices with minimum width below 600dp (for instance 480×800 dp)
layout-sw600dp – for all devices with minimum width above 600dp (for instance 1024×600 dp)
layout-sw720dp – for all devices with minimum width above 720dp (for instance 1280×800 dp)
This allows you to create dedicated layout files for them.