Android的集查看知名度编程根据屏幕大小知名度、屏幕、根据、大小

2023-09-06 10:36:41 作者:正宗二货·仿冒必究

我在我的Andr​​oid应用程序的视图,我想之间切换可见 / 消失在较小的屏幕和在较大规模的可见 / 无形。初始设置(消失为小,无形为大屏幕)是由具有在两个单独的XML布局文件来完成布局布局sw600dp土地,但后来当我需要动态交换可见性设置,我怎么能确定从Java code中哪一个挑根据屏幕大小?

I have a view in my android app that I would like to toggle between visible/gone on smaller screens, and visible/invisible in larger sizes. The initial set up (gone for small, invisible for large screens) is done by having two separate XML layout files under layout and layout-sw600dp-land, but then when I need to dynamically swap the visibility setting, how can I determine from within Java code which one to pick based on screen size?

编辑:更具体地说,我想在我的code,导致Android的使用从布局布局sw600dp土地在相同的条件来检测。我在想,即使记录值某处价值观sw600dp土地目录,但不知道放在哪个文件它变成以及如何访问它。

more specifically, I want to detect in my code the same condition that causes Android to use layouts from layout-sw600dp-land. I was thinking even recording the value somewhere in the values-sw600dp-land directory, but not sure which file to put it into and how to access it.

推荐答案

您可以使用以下屏幕的像素获得的大小。

You can get the size in pixels of the screen using the following.

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels; 
int height = dm.heightPixels; 

不过,你的问题含糊不清,是否屏幕尺寸意味着像素或英寸。您可能需要采取的dm.densityDpi值的优势,将值从像素转换为英寸,在屏幕的大小的一个更有用的计算。

However, your question was ambiguous as to whether size of screen meant pixels or inches. You may need to take advantage of the dm.densityDpi value, to convert the values from pixels to inches, for a more useful calculation of the "size" of the screen.

ANSER进行编辑:

ANSER FOR EDITS:

有两种可能的解决方案。一个是在这个线程引用,很简单,你提到它上面。

There are two potential solutions. One is referenced in this thread, very simple and you alluded to it above.

How以编程方式确定我的Andr​​oid应用程序正在使用的XML布局?

第二个是不是一个解决办法,而是一个解释。布局-sw600dp土地文件替换旧的命名约定pre 3.2的去这样的布局XLARGE土地。这实质上鬃毛在风景模式XLARGE画面。所以,你可以通过查找XLARGE屏幕尺寸,在宽度>高度编程方式检测这一点。下面是一个很好的参考,比较旧的约定VS新的sw600dp=最小宽度600 DP约定。

The second isn't a solution, but rather an explanation. The layout-sw600dp-land file replaces an old naming convention pre 3.2 that went like this layout-xlarge-land. This essentially manes "xlarge" screen in "landscape" mode. So you can detect this programmatically by finding xlarge screen sizes, in which the width > height. Below is a good reference to compare the old convention vs the new "sw600dp" = smallest width is 600 dp convention.

http://developer.android.com/training/multiscreen/screensizes.html