问题:COMPLEX_UNIT_PX

本文探讨了在Android应用开发中,使用SP与PX作为文本大小单位的区别。通过具体实例对比了这两种单位设置字体大小的不同效果,并解释了为何采用SP单位会导致字体显示更大。

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

现在获取文字大小等都是通过pixel,如下

textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.txt_x_small));

但是在TypedValue中还有另一个filedCOMPLEX_UNIT_SP,

textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.txt_x_small));

这样写会使得字变得很大,为什么?

我说了只要相对分布不要擅自修改:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp" android:background="#F5F5F5"> <LinearLayout android:layout_width="396dp" android:layout_height="22dp" android:background="#E0E0E0" android:gravity="center_vertical" android:orientation="horizontal"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end" android:orientation="horizontal"> <ImageView android:layout_width="16dp" android:layout_height="16dp" android:contentDescription="Signal" android:src="@drawable/ic_signal" /> <ImageView android:layout_width="16dp" android:layout_height="16dp" android:layout_marginLeft="4dp" android:contentDescription="Battery" android:src="@drawable/ic_battery" /> </LinearLayout> </LinearLayout> <ImageView android:layout_width="48dp" android:layout_height="48dp" android:src="@drawable/Deco_App" android:contentDescription="Deco Logo" android:layout_gravity="center" android:layout_marginVertical="16dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Welcome to Deco" android:textSize="20sp" android:textStyle="bold" android:textColor="#666666" android:layout_gravity="center" android:layout_marginBottom="16dp"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="I accept the Terms of Use and confirm that I have fully read and understood the Privacy Policy." android:textSize="12sp" android:textColor="#666666" android:buttonTint="#00C4CC" android:layout_marginBottom="8dp"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="I confirm to join the User Experience Improvement Program. I understand that I can opt out of the program any time." android:textSize="14sp" android:textColor="#666666" android:buttonTint="#00C4CC" android:layout_marginBottom="16dp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Continue" android:textColor="#FFFFFF" android:background="@drawable/rounded_button" android:layout_gravity="center" android:textSize="16sp" android:layout_marginBottom="8dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Disagree and Quit" android:layout_gravity="center" android:textSize="14sp" android:textColor="#00C4CC" /> </LinearLayout>
08-01
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值