简体   繁体   English

根据Android中的屏幕/片段宽度在TableLayout中隐藏列

[英]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). 我希望我的Android应用程序能够流畅地适应不同的屏幕分辨率和旋转(横向/纵向方向更改)。 So far using Fragments and several layout files has done the job, as recommended . 到目前为止,按照建议使用Fragments和几个布局文件已经完成了工作。

Now I am displaying database query results in a table format using TableLayout . 现在,我使用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. 我猜,最简单的方法是为不同的屏幕分辨率创建不同的TableLayouts。 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. 无论如何,建议在Android上采用适合不同屏幕尺寸和分辨率的不同布局。

added: What you can do is write your own MyTableLayout, and determine the number of columns on the fly. 补充:您可以做的是编写自己的MyTableLayout,并即时确定列数。 It's not that hard to write a view, and implement "onLayout" and "onMeasure". 编写视图并实现“ onLayout”和“ 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. 该视图将接受该行中的所有项目,并且仅显示适合的项目。

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

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