TableLayout嵌套TableRow中TextView文字显示不全,只是单行显示,不能换行。

本文介绍了一种在TabLayout下的TableRow中使用两个TextView时,当输入文本过长导致显示不全的问题解决办法。通过设置TextView宽度为0dp并使用权重为1的方法,可以确保即使文本过长也能正常显示。

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

TabLayout 下面的 TableRow中有2个TextView 如果输入的文本过长,会导致显示不全.


解决办法: 对TextView宽度使用权重百分比即可。

android:layout_width="0dp"

android:layout_weight="1"

private void addTableRow(TableLayout table, Object[] rowData) { TableRow row = new TableRow(requireContext()); // 设置行布局参数(正确方式) TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams( TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT ); row.setLayoutParams(rowParams); // 设置最小行高(单位:dp) int minRowHeight = dpToPx(36); row.setMinimumHeight(minRowHeight); for (Object data : rowData) { TextView textView = new TextView(requireContext()); textView.setText(String.valueOf(data)); textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14); // 设置单元格内边距 int padding = dpToPx(8); textView.setPadding(padding, padding/2, padding, padding/2); // 设置单行显示 textView.setSingleLine(true); textView.setEllipsize(TextUtils.TruncateAt.END); // 创建列布局参数(正确方式) TableRow.LayoutParams colParams = new TableRow.LayoutParams( 0, // 宽度将由权重控制 TableRow.LayoutParams.MATCH_PARENT ); // 设置权重和最小宽度 colParams.weight = 1; textView.setLayoutParams(colParams); // 设置文本视图的最小宽度(这才是正确的属性) textView.setMinWidth(dpToPx(120)); // 设置最小列宽 row.addView(textView); } table.addView(row); } // 动态添加表头 private void addTableHeader(TableLayout table) { TableRow headerRow = new TableRow(requireContext()); headerRow.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.purple_500)); TableLayout.LayoutParams headerParams = new TableLayout.LayoutParams( TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT ); headerRow.setLayoutParams(headerParams); String[] headers = {"订单号", "产品编号", "产品数量", "组件名", "板材信息", "板材/组件", "订购数量"}; for (String header : headers) { TextView headerView = new TextView(requireContext()); headerView.setText(header); headerView.setTextColor(Color.WHITE); headerView.setTypeface(null, Typeface.BOLD); headerView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16); int padding = dpToPx(8); headerView.setPadding(padding, padding, padding, padding); // 设置列布局参数 TableRow.LayoutParams colParams = new TableRow.LayoutParams( 0, TableRow.LayoutParams.MATCH_PARENT ); colParams.weight = 1; headerView.setLayoutParams(colParams); // 设置表头列的最小宽度 headerView.setMinWidth(dpToPx(120)); headerRow.addView(headerView); } table.addView(headerRow); } // DP转PX工具方法 private int dpToPx(int dp) { return (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics() ); }<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 水平滚动容器(关键:宽度设为match_parent) --> <HorizontalScrollView android:id="@+id/scroll_horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true" android:scrollbars="horizontal"> <!-- 表格容器(高度为wrap_content,宽度设为可无限扩展) --> <TableLayout android:id="@+id/orderTable" android:layout_width="wrap_content" android:layout_height="wrap_content" android:stretchColumns="*" android:shrinkColumns="*" android:paddingEnd="16dp" android:minWidth="1000dp"> <!-- 设置最小宽度防止初始压缩 --> <!-- 表格标题行(已移除原始代码中的标题行,将在Java中动态添加) --> </TableLayout> </HorizontalScrollView> <!-- 垂直滚动指示器 --> <View android:layout_width="4dp" android:layout_height="match_parent" android:layout_gravity="right" android:background="#80000000" android:visibility="invisible" android:id="@+id/vertical_scroll_indicator"/> </FrameLayout> textView.setSingleLine(true);单行忽略,动态调整长度,总长可以延伸
最新发布
06-07
private void addTableRow(TableLayout table, Object[] rowData) { TableRow row = new TableRow(requireContext()); Log.d(“log”,rowData[0].toString()); for (Object data : rowData) { TextView textView = new TextView(requireContext()); textView.setText(String.valueOf(data)); textView.setPadding(8, 12, 8, 12); row.addView(textView); } table.addView(row);<?xml version=“1.0” encoding=“utf-8”?> <androidx.core.widget.NestedScrollView xmlns:android=“http://schemas.android.com/apk/res/android” android:layout_width=“match_parent” android:layout_height=“match_parent”> <!-- 双滚动容器实现 --> <HorizontalScrollView android:layout_width=“wrap_content” android:layout_height=“wrap_content” android:fillViewport=“true”> <TableLayout android:id="@+id/orderTable" android:layout_width="wrap_content" android:layout_height="wrap_content" android:stretchColumns="*" android:shrinkColumns="*" android:paddingEnd="16dp"> <!-- 表格标题行 --> <TableRow android:background="?attr/colorPrimary" android:padding="8dp"> <TextView android:text="订单号" android:textColor="@android:color/white" android:padding="8dp" android:textStyle="bold"/> <TextView android:text="产品编号" android:textColor="@android:color/white" android:padding="8dp" android:textStyle="bold"/> <TextView android:text="产品数量" android:textColor="@android:color/white" android:padding="8dp" android:textStyle="bold"/> <TextView android:text="组件名" android:textColor="@android:color/white" android:padding="8dp" android:textStyle="bold"/> <TextView android:text="板材信息" android:textColor="@android:color/white" android:padding="8dp" android:textStyle="bold"/> <TextView android:text="板材/组件" android:textColor="@android:color/white" android:padding="8dp" android:textStyle="bold"/> <TextView android:text="订购数量" android:textColor="@android:color/white" android:padding="8dp" android:textStyle="bold"/> </TableRow> </TableLayout> </HorizontalScrollView> </androidx.core.widget.NestedScrollView> }如何解决载入数据后表格变形,单行可以变形但不能上下变形上下距离一个字符,横向可以超出页面并页面可以上下左右滚动
06-07
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值