不同的分辨率支持机器人

编辑问题:

移动分辨率: < br > 我想设计不同的屏幕 dpi 像下面的分辨率。
320x480,< br > 480 × 800,< br > 540x960,< br > 720x1280(Samsung S3) ,< br > 1080x1920(S4,Nexus5,Nexus 5x,Moto G4) ,< br > 2560x1440(Nexus 6,Nexus 6p,Samsung edge) < br >
平板分辨率: < br > 480x800(micromax) ,< br > 600x1024(Samsung tab2) ,< br > 800x1280(nexus 7) ,< br > 1200x1920(new nexus 7) ,< br > 2048x1536(nexus 9)

我想使用 不同的字体大小取决于设备的显示分辨率。

问题1)解决这个问题的 best方法是什么?

Q2)抛编码和 XML 的最佳选择是什么?

Q3)哪个可绘制文件夹代表哪个设备分辨率?

Q4)不同分辨率的应用程序启动器图标大小?

100371 次浏览

首先为一个分辨率设计应用程序。

假设你的手机分辨率是380 * 480

       mobile screen width:380


textView size is 80dp
Assume : if width is 380dp then 100 % then


textview width 80dp then how many %(per).


ower answer is: 25 %

使用 Belove 公式以编程方式查找屏幕大小

    DisplayMetric  displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
ScreenHeight = displaymetrics.heightPixels;
ScreenWidth = displaymetrics.widthPixels;
txt_height = (int)((ScreenHeight*14.58)/100);
txt_width = (int)((ScreenWidth*37.5)/100);




LinearLayout.LayoutParams normal_param = new LinearLayout.LayoutParams(txt_height ,txt_width );


txt_Name.setLayoutParams(normal_param);

基本上你需要创建一个文本样式,如下所示:

<style name="CodeFont">
<item name="android:textSize">30sp</item>
</style>

详情请浏览此处

使用 android 对不同屏幕的支持指南,在正确的分辨率文件夹中为不同屏幕创建所需的不同尺寸,如下所示: http://developer.android.com/guide/practices/screens_support.html

附注: 我真的不明白在什么情况下你会想这样做,使用 SP 单位的字体大小将缩放字体看起来差不多相同的大小在不同的手机。

我认为这是一个很好的答案:

文本大小和不同的安卓屏幕大小

但在这里你可以用屏幕分辨率做到这一点:

您可以为每个分辨率创建“ value”资源目录,如

values-wWIDTHp-hHEIGHTdp (you can also use values-wWIDTHp or values-hHEIGHTdp)
for example: 320*480 will be values-w320p-h480dp

在每个 dir (包括默认值 dir)中创建一个名为“  目標 s.xml”的文件,其内容如下:

 for exmaple (the value related to the resolution):
<dimen name="def_font_size">10sp</dimen>

现在可以使用“@dimen/def _ font _ size” 或在默认值目录中创建样式。

将其添加到“ styles.xml”中:

<style name="FontSize">
<item name="android:textSize">@dimen/def_font_size</item>
</style>

应用程序启动程序图标大小,以不同分辨率为单位

移动分辨率

  • Mipmap-mdpi (48X48)
  • Mipmap-hdpi (72X72)
  • Mipmap-xhdpi (96X96)
  • Mipmap-xxhdpi (144X144)
  • Mipmap-xxxhdpi (192X192)

平板电脑布局:

如果你想要平板电脑特有的布局,请使用以下文件夹:

layout-large-mdpi   (1024x600)
layout-large-tvdpi  (800x1280)
layout-large-xhdpi  (1200x1920)
layout-xlarge-mdpi  (1280x800)
layout-xlarge-xhdpi (2560x1600)

可绘制文件夹:

  1. 手机

    res/drawable        (default)
    res/drawable-ldpi/  (240x320 and nearer resolution)
    res/drawable-mdpi/  (320x480 and nearer resolution)
    res/drawable-hdpi/  (480x800, 540x960 and nearer resolution)
    res/drawable-xhdpi/  (720x1280 - Samsung S3, Micromax Canvas HD etc)
    res/drawable-xxhdpi/ (1080x1920 - Samsung S4, HTC one, Nexus 5, etc)
    res/drawable-xxxhdpi/ (1440X2560 - Nexus 6,Samsung S6edge).
    
  2. Tablet Resolution: enter image description here

    Font Sizes:

NOTE: Always try to use SP whenever you deal with textSize, like textsize=12sp

  1. Use predefined textAppearance:

    It will set text size automatically as per device density.

    <TextView android:textAppearance="?android:attr/textAppearanceSmall"/>
    <TextView android:textAppearance="?android:attr/textAppearanceMedium"/>
    <TextView android:textAppearance="?android:attr/textAppearanceLarge" />
    

    使用方法:

    <TextView
    style="@android:style/TextAppearance.Small"
    android:text="Sample Text - Small" />
    <TextView
    style="@android:style/TextAppearance.Medium"
    android:text="Sample Text  - Medium" />
    <TextView
    style="@android:style/TextAppearance.Large"
    android:text="Sample Text  - Large" />
    
  2. Use dimension.xml for each device:

    From Google IO Pdf, we see structure below:

    1. Mobile:

      res/values/dimens.xml(default)
      res/values-ldpi/dimens.xml   (240x320 and nearer resolution)
      res/values-mdpi/dimens.xml   (320x480 and nearer resolution)
      res/values-hdpi/dimens.xml   (480x800, 540x960 and nearer resolution)
      res/values-xhdpi/dimens.xml  (720x1280 - Samsung S3, Micromax Canvas HD, etc)
      res/values-xxhdpi/dimens.xml (1080x1920 - Samsung S4, HTC one, etc)
      

      Res/value-xxxhdpi/ xml (1440X2560-Nexus 6,Samsung S6edge).

    2. 平板电脑:

      对于平板电脑,你可以使用更具体的文件夹,如 values-xlargevalues-large

      res/values-large/dimens.xml      (480x800)
      res/values-large-mdpi/dimens.xml (600x1024)
      

      或者

      res/values-sw600dp/dimens.xml      (600x1024)
      res/values-sw720dp/dimens.xml      (800x1280)
      res/values-xlarge-xhdpi/dimens.xml (2560x1600 - Nexus 10")
      res/values-large-xhdpi/dimens.xml  (1200x1920 - Nexus 7"(latest))
      

For further information:

  1. Refer to Supporting Multiple Screens.

  2. See Page# 77 of Google IO Pdf for Design device density. In that, you will find the way to handle dimens.xml for different different devices.

  3. Getting Your Apps Ready for Nexus 6 and Nexus 9.

Excerpt from Supporting Multiple Screens:

The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.

首先,为不同的屏幕创建不同的值文件夹 在 res->values->dimens.xml文件和调用简单的字体大小使用 "@dimen/text_size"

res/values/dimens.xml
res/values-small/dimens.xml
res/values-normal/dimens.xml
res/values-xlarge/dimens.xml


//for small
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">15sp</dimen>
</resources>


//for normal
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">20sp</dimen>
</resources>


//for large
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">30sp</dimen>
</resources>


//for xlarge
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">40sp</dimen>
</resources>

检索 TextView中的字体大小,如下所示。

<TextView
android:id="@+id/lblHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/text_size"
android:textStyle="bold"
android:typeface="serif" />

为了避免不同屏幕分辨率和不同密度的问题,我根据屏幕的百分比宽度和高度来调整大小和位置。如果根据屏幕的百分比宽度或高度调整文本大小,则所有设备和所有分辨率上的字体大小都是正确的。要根据空间的宽度和高度获得正确的字体大小,只需使用以下函数:

private float SetTextSize(String text, int width, int height)
{
Paint paint = new Paint();
float textWidth = paint.measureText(text);
float textSize = (int) ((width / textWidth) * paint.getTextSize());
paint.setTextSize(textSize);


textWidth = paint.measureText(text);
textSize = (int) ((width / textWidth) * paint.getTextSize());


// Re-measure with font size near our desired result
paint.setTextSize(textSize);


// Check height constraints
FontMetricsInt metrics = paint.getFontMetricsInt();
float textHeight = metrics.descent - metrics.ascent;
if (textHeight > height)
{
textSize = (int) (textSize * (height / textHeight));
paint.setTextSize(textSize);
}
return textSize;
}

下面是获取屏幕宽度和高度的代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2)
{
Point size = new Point();
getWindowManager().getDefaultDisplay().getSize(size);
screenWidth = size.x;
screenHeight = size.y;
}
else
{
Display display = getWindowManager().getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
}

要获得所需的字体大小,只需根据屏幕宽度和屏幕高度创建一个区域,并对其进行调整,直到字体大小看起来正确。一旦你让它看起来正确,它应该看起来正确的所有设备。

float textSize = SetTextSize("text", (int) (screenWidth * 0.1), (int) (screenHeight * 0.15));

我希望这能帮到你

我已经创建了一个函数,转换大小的 dp 的大小根据屏幕大小,它是工作良好的我。任何一个有问题的文本大小根据屏幕应该给这一个尝试。

public float convertFromDp(int input) {
final float scale = getResources().getDisplayMetrics().density;
return ((input - 0.5f) / scale);
}

Simple 通过编程方式为文本视图大小赋值,如下所示

tvTextView1.setTextSize(convertFromDp(24));

对于 Nexus6设备支持,创建可绘制的 -560dpi 文件夹并将所有图像放入其中。