开发者

Control tablet / phone layout on 3.2+

开发者 https://www.devze.com 2023-03-30 05:37 出处:网络
3.2 introduced a great new way to create layouts... by using layout-swXXXdp in the res folder. The problem is it controls what layout is used by the SmallestWidth in dp. This is great, but if you have

3.2 introduced a great new way to create layouts... by using layout-swXXXdp in the res folder. The problem is it controls what layout is used by the SmallestWidth in dp. This is great, but if you have a tablet that's 1024x600 and a tablet that's 1024x768, there is an issue. 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. All screens that are 1024 pixels should have 5 items in a gridvie开发者_开发知识库w row, while all tablets that are 1280 pixels should have 6 items in a gridview row. It should not be based on width because then the gridview gets a little squished. 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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消