简体   繁体   中英

Two columns for ListView or TableLayout in Landscape mode

How can I get one column in portrait mode and two columns in landscape mode with a ListView or TableLayout .

For example, in Portrait:

  • Element 1
  • Element 2
  • Element 3
  • Element 4

And in Landscape :

  • Element1 Element2
  • Element3 Element4

I thought to use ListView with one column in portrait mode and TableLayout with two columns in landscape mode, but before that, I want to know your suggestions.

I recommend using a ListView rather than a TableLayout, since ListViews only create enough Views to fill the screen while a TableLayout will create every resource whether they are visible or not. In this way a TableLayout is slower and might even crash your app.


You can check the device's orientation with one of the answers from: Check orientation on Android phone . Then if the orientation is:

  • landscape, load your double View layout.
  • portrait, load your basic, single View layout.

Loading different layouts in a ListView requires a simple custom Adapter. If you haven't written one before, here is a tutorial to help you.

It'd be way easy for you to use a GridView. You can set/choose number of columns on runtime and hence set them differently in portait vs landscape mode. That way, you'd just be manipulating a single view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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