简体   繁体   中英

Hide columns in TableLayout based on screen/fragment width in Android

I want my Android application to adapt fluidly to different screen resolutions and rotations (landscape/portrait orientation changes). So far using Fragments and several layout files has done the job, as recommended .

Now I am displaying database query results in a table format using TableLayout .

To adapt this layout for different screen/layout widths, I would like to completely hide columns (hiding the least-important columns first) until there is room enough to fit what's left over in the available space. Ideally cell contents will not wrap, and column widths will be fixed at the minimum required to display their contents entirely.

Note that last part: as column widths are a function of their contents, and aren't known at design time, then the layout must adapt at run time to fit the space available, and column widths are not allowed to shrink.

The easiest way to do this, I guess, would to create different TableLayouts for different screen resolutions. In your code, you would test if a certain field is available in the layout, and fill it if it is. If it's not, than you are on a device that has a screen resolution that doesn't have the column for that particular field. Having different layouts for different screen sizes and resolutions is the recommended way on Android anyway.

added: What you can do is write your own MyTableLayout, and determine the number of columns on the fly. It's not that hard to write a view, and implement "onLayout" and "onMeasure". That way, you'll always have the best fit. The view would accept all items in the row, and it would only show those that fit.

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