简体   繁体   English

一个应用程序如何处理屏幕分辨率和屏幕尺寸

[英]how to handle screen resolutions and screen sizes, one app

I have been trying to figure this out but I really dont get it.. If I want to create one app with one layout thats supposed to fit all android devices (by scaling).. how can I achive this?.. For instance.. I might have an editText thats 300dp, that will look great on my Samsung Galaxy S2.. but if I run the same app at my nexus 10, it will look very small, and I whould then like the editText to scale up on both width and height to fit my nexus 10.. is there any simple way to achive this? 我一直在试图解决这个问题,但我真的不明白。.如果我想创建一个应用程序,且该应用程序的布局应该适合所有android设备(通过缩放)..我如何才能做到这一点?我可能有一个300dp的editText,在我的Samsung Galaxy S2上看起来不错。.但是,如果我在nexus 10上运行相同的应用程序,它看起来会很小,然后我希望editText在两者上都可以放大宽度和高度以适合我的关系10 ..有没有简单的方法可以达到此目的?

I noticed that there is something called weight.. but if i created three editTexts where the first one had a weight of 10,the 2:nd a weight of 20 and then 3:rd a weight of 80, the last one never showed up... 我注意到有一个叫做权重的东西。但是,如果我创建了三个editText,其中第一个的权重为10,第二个的权重为20,然后第三的权重为80,则最后一个永远不会出现...

Anyway.. how do you handle this stuff by best practice? 无论如何..您如何通过最佳实践来处理这些东西?

Generally this situation can be handled by adding 通常,这种情况可以通过添加

<supports-screens android:normalScreens="true"
    android:smallScreens="true" android:largeScreens="true"
    android:anyDensity="true" android:resizeable="true" />

in the androidmanifest.xml 在androidmanifest.xml中

Use the xhdpi resources and try to set all the layouts at the runtime using the layout params according to the device. 使用xhdpi资源,并尝试在运行时根据设备使用布局参数设置所有布局。

or let the android decide what is best for the device create following folders 或让android决定最适合该设备的设备创建以下文件夹

  1. layout-large 布局大
  2. layout-small 布局小
  3. layout-sw600 布局-sw600
  4. layout-sw720 布局-sw720

and similar folders to organize your layouts better 和类似的文件夹可以更好地组织您的布局

This document would be great help 该文档将对您有很大帮助

http://developer.android.com/guide/practices/screens_support.html http://developer.android.com/guide/practices/screens_support.html

If you your layout to look good on both phone and tablet, you will need to have separate layout files, one in layout and another in layout-xlarge (for the 10" tablet), put in different size for the edittext. 如果您的布局在手机和平​​板电脑上看起来都不错,则需要有单独的布局文件,一个在布局中,另一个在layout-xlarge中(对于10“平板电脑),并为编辑文本放置不同的大小。

Another approach is to keep a single layout file but extract all the size related value to separate xml files. 另一种方法是保留单个布局文件,但将所有与大小相关的值提取到单独的xml文件中。 Put them in values and values-xlarge folders respectively. 将它们分别放在values和values-xlarge文件夹中。

Here is how I do it, 这是我的方法

Create a function that you can call that returns device width dependent size. 创建一个可以调用的函数,该函数返回与设备宽度有关的大小。 The "32" is arbitrary which gives you a relative font size proportional to the screen. “ 32”是任意的,它为您提供与屏幕成比例的相对字体大小。

textView.setTextSize(getFontSize(activity));


public static int getFontSize (Activity activity) { 

    DisplayMetrics dMetrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(dMetrics);

    // lets try to get them back a font size realtive to the pixel width of the screen
    final float WIDE = activity.getResources().getDisplayMetrics().widthPixels;
    int valueWide = (int)(WIDE / 32.0f / (dMetrics.scaledDensity));
    return valueWide;
}

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

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