简体   繁体   English

Android:为不同的设备/屏幕尺寸设置不同尺寸的神秘技巧

[英]Android : the mystical technique to set different dimensions for different device / screen sizes

My first Android App. 我的第一个Android应用程序。 And, am trying to get this sorted -> setting different text sizes, layout sizes for different screen / device sizes. 而且,我试图将其排序->为不同的屏幕/设备尺寸设置不同的文本大小,布局大小。 Now, I Googl'ed a lot , then read several Stackoverflow threads, & am doing this: Created different folders under 'res' like values-ldpi values-mdpi values-hdpi values-xhdpi values-xxhdpi values-xxxhdpi 现在,我进行了很多操作,然后阅读了几个Stackoverflow线程,然后执行此操作:在“ res”下创建了不同的文件夹,例如值-ldpi值-mdpi值-hdpi值-xhdpi值-xxhdpi值-xxxhdpi

Then I put a dimens.xml & styles.xml in each folder above. 然后,在上面的每个文件夹中放入dimens.xml和styles.xml。

Phew! But this doesn't suffice. 但这还不够。 I want to have different textsizes (and other dimensions, am not speaking about images here) for phones & different ones for tablets. 我想为手机设置不同的文本大小(以及其他尺寸,这里不谈论图像),为平板电脑设置不同的文本大小。 I know there are these 'values-sw600dp, and 'values-sw720dp' which they say is aimed at tablets. 我知道这些“ values-sw600dp”和“ values-sw720dp”是针对平板电脑的。 But even within each tablet size, I need to differentiate the different densities right? 但是即使在每种平板电脑尺寸内,我也需要区分不同的密度,对吗? For example, within 'values-sw600dp', I need to have different textsizes for, say, hdpi, xhdpi, ... How do I do this? 例如,在“ values-sw600dp”中,我需要为hdpi,xhdpi等设置不同的文本大小。 Any help please? 有什么帮助吗?

Here is an example of what am doing: 这是正在执行的示例:

    TextView textView = (TextView) findViewById(R.id.text_interference);
    textView.setHeight(layoutHt);
    textView.setWidth((layoutWd-20)/2);
    int dimensControlButtonTextSize = getResources().getInteger(R.integer.CONTROL_BUTTON_TEXT_SIZE);
    textView.setTextSize(dimensControlButtonTextSize);

Then in dimens.xml, it would be: 17 然后在dimens.xml中,它将是:17

This value, ie 17 here, will differ in each device ie it is given a different value in each dimens.xml under each values- folder. 在每个设备中,此值(即此处的17)将有所不同,即,在每个values-文件夹下的dimens.xml中为其指定了不同的值。 I hope am clear. 我希望很清楚。

Drawable resources do not need to be duplicated per screen configuration, except for special cases like you have a full screen splash that would be displayed differently for landscape and portrait. 可绘制资源不需要按每个屏幕配置重复,除非特殊情况(例如全屏启动时,横向和纵向显示方式不同)除外。 Normally for icons, specifying once in the main resources folder (under hdpi, mdpi, xhdpi, xxhdpi...) should be sufficient. 通常对于图标,在主资源文件夹中指定一次(在hdpi,mdpi,xhdpi,xxhdpi ...下)就足够了。

I would find screen configuration specific resources to be useful for dimen values, like padding, margin, width of items, etc... 我会觉得屏幕配置特定的资源为有用dimen值,比如填充,保证金,项目的宽度,等...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM