简体   繁体   English

在3.2+上控制平板电脑/手机的布局

[英]Control tablet / phone layout on 3.2+

3.2 introduced a great new way to create layouts... by using layout-swXXXdp in the res folder. 3.2通过使用res文件夹中的layout-swXXXdp引入了一种创建布局的绝佳新方法。 The problem is it controls what layout is used by the SmallestWidth in dp. 问题在于它控制dp中的SmallestWidth使用什么布局。 This is great, but if you have a tablet that's 1024x600 and a tablet that's 1024x768, there is an issue. 很棒,但是如果您使用的平板电脑为1024x600,而平板电脑的分辨率为1024x768,则存在问题。 My app is landscape only. 我的应用程序仅适用于风景。 It is a gridview with a certain number of items in a row. 它是一个网格视图,其中连续有一定数量的项目。 The problem is that I need the gridview to adjust how many items are in a row based on the height resolution. 问题是我需要gridview根据高度分辨率调整一行中有多少个项目。 All screens that are 1024 pixels should have 5 items in a gridview row, while all tablets that are 1280 pixels should have 6 items in a gridview row. 所有1024像素的屏幕在gridview行中应该有5项,而所有1280像素的平板电脑在gridview行中应该有6项。 It should not be based on width because then the gridview gets a little squished. 它不应该基于宽度,因为这样gridview会变得有些变形。 How can I do this? 我怎样才能做到这一点? (base my layout choise on the height, not width) (我的布局选择基于高度,而不是宽度)

Please try this 请尝试这个

Get Height and Width of the Display 获取显示器的高度和宽度

Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();

Check the height after declaration of GridView and set Number of columns accordingly 声明GridView后检查高度,并相应地设置列数

For example 例如

GridView gridView = (GridView)findViewById(R.id.app_gridview);
if(height <= 1024){
   gridView.setNumColumns(5);
}else {
   gridView.setNumColumns(6);
}

I think this will solve the problem 我认为这可以解决问题

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

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