getHeight/getTop

本文详细介绍了Android开发中如何使用getHeight()方法获取控件的高度,特别指出该高度是指控件在可视界面内的部分。同时解释了getTop()方法用于获取控件顶部与其父控件顶部之间的距离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

getHeight()得到的是当前控件的高度,这里需要强调的是:这个高度是可视界面的高度,也就是说这个高度肯定是小于等于屏幕的高度的。如果你的控件超出了屏幕,那么getHeight()只会获取屏幕内的高度。

getHeight必须在控件加载结束后才能够获取到高度,onCreate等方法中获取的高度为0,可以在onWindowFocusChanged方法中获取。

getTop,获取当前控件顶部距离父控件顶部的距离,这里强调的是:这个距离是当前控件到父控件,和屏幕的边缘没有关系。

AcroFields.FieldPosition tablePosition = form.getFieldPositions("orderTable").get(0); // 获取这个区域的矩形 Rectangle rect = tablePosition.position; float tableX = rect.getLeft(); float tableY = rect.getBottom(); float width = rect.getWidth(); // 注意:y坐标是从页面底部开始的,而我们的表格绘制是从顶部开始,所以需要计算表格高度 // 创建表格(假设4列:型号、数量、单价、总价) PdfPTable table = new PdfPTable(5); // 设置表格宽度 table.setTotalWidth(width); table.setWidthPercentage(100); // 宽度100%填充 // 关键设置:启用跨页自动分割 table.setSplitRows(true); // 默认即为true,可省略 table.setSplitLate(false); // 确保行不被拆分(整行移动到新页) // 添加表头 table.setHeaderRows(1); // 设置表头行数(跨页时自动重复) table.addCell(createCell("序号", true)); table.addCell(createCell("型号",true)); table.addCell(createCell("数量",true)); table.addCell(createCell("单价",true)); table.addCell(createCell("总价",true)); // 动态添加商品行 int index = 1; for (int i = 0; i < 20; i++) { table.addCell(createCell(String.valueOf(index++), false)); table.addCell(createCell("格力电器",false)); table.addCell(createCell("5",false)); table.addCell(createCell("1200",false)); table.addCell(createCell("6000",false)); } // 5. 在指定位置绘制表格 // 获取当前页的OverContent(注意:表格位置在goodsTable所在的页) PdfContentByte canvas = pdfStamper.getOverContent(tablePosition.page); float tableNY = rect.getTop(); // 预留区域的顶部(即y+rect.getHeight()) table.writeSelectedRows(0, -1, tableX, tableNY, canvas);当绘制表格内容过多,怎么跨行分页继续绘制?
最新发布
07-24
AcroFields.FieldPosition tablePosition = form.getFieldPositions("orderTable").get(0); // 获取这个区域的矩形 Rectangle rect = tablePosition.position; float tableX = rect.getLeft(); float tableY = rect.getBottom(); float width = rect.getWidth(); // 注意:y坐标是从页面底部开始的,而我们的表格绘制是从顶部开始,所以需要计算表格高度 // 创建表格(假设4列:型号、数量、单价、总价) PdfPTable table = new PdfPTable(5); // 设置表格宽度 table.setTotalWidth(width); table.setWidthPercentage(100); // 宽度100%填充 // 关键设置:启用跨页自动分割 table.setSplitRows(true); // 默认即为true,可省略 table.setSplitLate(false); // 确保行不被拆分(整行移动到新页) // 添加表头 table.setHeaderRows(1); // 设置表头行数(跨页时自动重复) table.addCell(createCell("序号", true)); table.addCell(createCell("型号",true)); table.addCell(createCell("数量",true)); table.addCell(createCell("单价",true)); table.addCell(createCell("总价",true)); // 动态添加商品行 int index = 1; for (int i = 0; i < 20; i++) { table.addCell(createCell(String.valueOf(index++), false)); table.addCell(createCell("格力电器",false)); table.addCell(createCell("5",false)); table.addCell(createCell("1200",false)); table.addCell(createCell("6000",false)); } // 5. 在指定位置绘制表格 // 获取当前页的OverContent(注意:表格位置在goodsTable所在的页) PdfContentByte canvas = pdfStamper.getOverContent(tablePosition.page); float tableNY = rect.getTop(); // 预留区域的顶部(即y+rect.getHeight()) table.writeSelectedRows(0, -1, tableX, tableNY, canvas);
07-24
for (int i = 0;i < dataLists.size();i++) { DataList.DataDTO 数据 = dataLists.get(i).getData(); List<DataList.DataDTO.ComponentsDTO> components = data.getComponents(); for (int j = 0;j < components.size();j++) { 字符串名称 = components.get(j).getName(); 类型 = components.get(j).getType(); Log.i(“组件:”, “” + 类型); setStyle = components.get(j).getSetStyle(); Log.i(“组件:”, 名称); //1文本组件 2图片组件 3视频组件 4天气 5日期 6线路 if (type == 1) { int 高度 = setStyle.getHeight(); int width = setStyle.getWidth(); int left = setStyle.getLeft(); int top = setStyle.getTop(); int marqueeWidth = Integer.parseInt(String.valueOf(width)); int marqueeHeight = Integer.parseInt(String.valueOf(height)); int marqueeTop = Integer.parseInt(String.valueOf(top)); Log.i(“aaaaaaa”, “文本组件marqueeTop:” + marqueeTop); LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tvMarquee.getLayoutParams(); params.width = dip2px(MainActivity.this, marqueeWidth); params.height = dip2px(MainActivity.this, marqueeHeight); params.leftMargin = dip2px(MainActivity.this, 左);// 设置左边距 params.topMargin = dip2px(MainActivity.this, marqueeTop);// 设置上边距 Log.i(“aaaaaaa”, “文本组件:” + type); } else if (type == 2) { int 高度 = setStyle.getHeight(); int width = setStyle.getWidth(); int left = setStyle.getLeft(); int top = setStyle.getTop(); int imageWidth = Integer.parseInt(String.valueOf(width)); int imageHeight = Integer.parseInt(String.valueOf(height)); int imageTop = Integer.parseInt(String.valueOf(top)); LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tvImage.getLayoutParams(); params.width = dip2px(MainActivity.this, imageWidth); params.height = dip2px(MainActivity.this, imageHeight); params.leftMargin = dip2px(MainActivity.this, left);// 设置左边距 params.topMargin = dip2px(MainActivity.this, imageTop);// 设置上边距 Log.i(“aaaaaaa”, “图片组件:” + type); } else if (type == 3) { int 高度 = setStyle.getHeight(); int width = setStyle.getWidth(); int left = setStyle.getLeft(); int top = setStyle.getTop(); int videoWidth = Integer.parseInt(String.valueOf(width)); int videoHeight = Integer.parseInt(String.valueOf(height)); int videoTop = Integer.parseInt(String.valueOf(top)); LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tvVideo.getLayoutParams(); params.width = dip2px(MainActivity.this, videoWidth); params.height = dip2px(MainActivity.this, videoHeight); params.leftMargin = dip2px(MainActivity.this, left);// 设置左边距 params.topMargin = dip2px(MainActivity.this, videoTop);// 设置上边距 Log.i(“aaaaaaa”, “视频组件:” + type); } else if (type == 4) { int 高度 = setStyle.getHeight(); int width = setStyle.getWidth(); int left = setStyle.getLeft(); int top = setStyle.getTop(); int weatherWidth = Integer.parseInt(String.valueOf(width)); int weatherHeight = Integer.parseInt(String.valueOf(height)); int weatherTop = Integer.parseInt(String.valueOf(top)); LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tvWeather.getLayoutParams(); params.width = dip2px(MainActivity.this, weatherWidth); params.height = dip2px(MainActivity.this, weatherHeight); params.leftMargin = dip2px(MainActivity.this, left);// 设置左边距 params.topMargin = dip2px(MainActivity.this, weatherTop);// 设置上边距 Log.i(“aaaaaaa”, “天气组件:” + type); } else if (type == 5) { int 高度 = setStyle.getHeight(); int width = setStyle.getWidth(); int left = setStyle.getLeft(); int top = setStyle.getTop(); int dateWidth = Integer.parseInt(String.valueOf(width)); int dateHeight = Integer.parseInt(String.valueOf(height)); int dateTop = Integer.parseInt(String.valueOf(top)); LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tvDate.getLayoutParams(); params.width = dip2px(MainActivity.this, dateWidth); params.height = dip2px(MainActivity.this, dateHeight); params.leftMargin = dip2px(MainActivity.this, left);// 设置左边距 params.topMargin = dip2px(MainActivity.this, dateTop);// 设置上边距 Log.i(“aaaaaaa”, “日期组件:” + type); } else if (type == 6) { int 高度 = setStyle.getHeight(); int width = setStyle.getWidth(); int left = setStyle.getLeft(); int top = setStyle.getTop(); int listWidth = Integer.parseInt(String.valueOf(width)); int listHeight = Integer.parseInt(String.valueOf(height)); int listTop = Integer.parseInt(String.valueOf(top)); LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tvList.getLayoutParams(); params.width = dip2px(MainActivity.this, listWidth); params.height = dip2px(MainActivity.this, listHeight); params.leftMargin = dip2px(MainActivity.this, left);// 设置左边距 params.topMargin = dip2px(MainActivity.this, listTop);// 设置上边距 Log.i(“aaaaaaa”, “线路组件:” + type); } } } <?xml 版本=“1.0” encoding=“utf-8”?> <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android” xmlns:app=“http://schemas.android.com/apk/res-auto” xmlns:tools=“http://schemas.android.com/tools” android:layout_width=“match_parent” android:layout_height=“match_parent” android:orientation=“vertical” tools:context=“ 的MainActivity”> <TextView android:id=“@+id/文本” android:layout_width=“wrap_content” 安卓:layout_height=“wrap_content” android:text=“1.0” android:textSize=“30sp” 安卓:layout_gravity=“center” android:visibility=“gone” /> <TextView android:id=“@+id/tv_marquee” android:layout_width=“match_parent” 安卓:layout_height=“50dp” android:background=“@color/黑色” android:text=“跑马灯” android:textColor=“@color/白色” android:textSize=“30sp” android:gravity=“center” /> <TextView android:id=“@+id/tv_video” android:layout_width=“match_parent” 安卓:layout_height=“260dp” android:background=“@color/select_color” android:text=“视频” android:gravity=“center” android:textColor=“@color/白色” android:textSize=“30sp” /> <LinearLayout android:layout_width=“match_parent” 安卓:layout_height=“wrap_content” android:orientation=“horizontal” <TextView android:id=“@+id/tv_date” 安卓:layout_width=“0dp” 安卓:layout_height=“50dp” 安卓:layout_weight=“1” android:background=“@color/common_botton_bar_blue” android:text=“时间日期” android:textColor=“@color/白色” android:gravity=“center” android:textSize=“30sp” /> <TextView android:id=“@+id/tv_weather” 安卓:layout_width=“0dp” 安卓:layout_height=“50dp” android:background=“@color/time_bg” 安卓:layout_weight=“1” android:text=“天气” android:textColor=“@color/白色” android:gravity=“center” android:textSize=“30sp” /> </LinearLayout> <TextView android:id=“@+id/tv_list” android:layout_width=“match_parent” android:layout_height=“match_parent” android:background=“@color/colorPrimary” android:text=“列表” android:textColor=“@color/白色” android:gravity=“center” android:textSize=“30sp” /> </LinearLayout>这段代码根据获取的值实现所有控件可以随便调换位置,怎么修改
06-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值