关于"match_parent"这个xml的布局设定值

本文详细解析了Android布局中match_parent与FILL_PARENT两个属性的区别及其在不同API级别中的应用情况。介绍了match_parent从API Level 8开始被引入,并取代了之前的FILL_PARENT,同时说明了该变化对于跨版本应用程序开发的影响。
android:layout_width="match_parent"

android:layout_height="match_parent"


"match_parent" 这个设置值 Since: API Level 8

public static final int MATCH_PARENT
Since: API Level 8

Special value for the height or width requested by a View. MATCH_PARENT means that the view wants to be as big as its parent, minus the parent's padding, if any. Introduced in API Level 8.

Constant Value: -1 (0xffffffff)

因此当你使用sdk2.2以下来编译时,编译会通不过!


再来看看下面这个

public static final int FILL_PARENT
Since: API Level 1

Special value for the height or width requested by a View. FILL_PARENT means that the view wants to be as big as its parent, minus the parent's padding, if any.This value is deprecated starting in API Level 8 and replaced byMATCH_PARENT.

Constant Value: -1 (0xffffffff)

也就是说,当你使用2.2以上的sdk编译成的app运行在2.2以下系统的手机上, "match_parent"的使用就赞同于使用了“full_parent”,因此这个值的设置对app的多os版本运行是没有影响的!!!



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
现在这个界面,地图占用空间过大。<?xml version="1.0" encoding="utf-8"?> <androidx.core.widget.NestedScrollView 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:fillViewport="true" android:fitsSystemWindows="true" android:layout_marginTop="?attr/actionBarSize"> <!-- 根布局:ConstraintLayout 必须 match_parent 高度才能被 NestedScrollView 正确测量 --> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 🔵 垂直指南线:5%、95% --> <androidx.constraintlayout.widget.Guideline android:id="@+id/guideline_05" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintGuide_percent="0.05" /> <androidx.constraintlayout.widget.Guideline android:id="@+id/guideline_15" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintGuide_percent="0.15" /> <androidx.constraintlayout.widget.Guideline android:id="@+id/guideline_95" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintGuide_percent="0.95" /> <!-- 🔍 输入框 --> <EditText android:id="@+id/search_input" android:layout_width="0dp" android:layout_height="wrap_content" android:hint="请输入需要查询的公交线路或站点" android:background="@drawable/rounded_edittext" android:minHeight="48dp" android:textSize="16sp" android:padding="12dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toStartOf="@id/search_btn" app:layout_constraintHorizontal_chainStyle="packed" app:layout_constraintTop_toTopOf="@id/guideline_05" app:layout_constraintBottom_toTopOf="@id/space_after_search" android:layout_marginStart="16dp" android:layout_marginEnd="8dp" /> <!-- 🔎 搜索按钮 --> <Button android:id="@+id/search_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="搜索" app:layout_constraintTop_toTopOf="@id/guideline_05" app:layout_constraintBottom_toTopOf="@id/space_after_search" app:layout_constraintStart_toEndOf="@id/search_input" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp" /> <!-- ⬛ Space: 搜索栏下方留出 5% 屏幕高度作为间隔 --> <Space android:id="@+id/space_after_search" android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintTop_toBottomOf="@id/guideline_15" app:layout_constraintBottom_toTopOf="@id/map_view" app:layout_constraintHeight_percent="0.05" /> <!-- 🗺️ 地图视图:占页面 40% 高度 --> <com.amap.api.maps.MapView android:id="@+id/map_view" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintTop_toBottomOf="@id/space_after_search" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHeight_percent="0.4" /> <!-- ⬛ Space: 地图到列表之间的空白 --> <Space android:id="@+id/space_after_map" android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintTop_toBottomOf="@id/map_view" app:layout_constraintBottom_toTopOf="@id/result_list" app:layout_constraintHeight_percent="0.03" /> <!-- 🔽 RecyclerView:结果列表,占据剩余空间 --> <androidx.recyclerview.widget.RecyclerView android:id="@+id/result_list" android:layout_width="0dp" android:layout_height="wrap_content" android:background="@drawable/rounded_edittext" app:layout_constraintTop_toBottomOf="@id/space_after_map" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toTopOf="@id/btn_go_to" android:layout_marginHorizontal="16dp" /> <!-- “到这去”按钮 --> <Button android:id="@+id/btn_go_to" android:layout_width="0dp" android:layout_height="wrap_content" android:text="到这去" android:layout_margin="16dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="@id/guideline_95" /> <!-- 🌀 加载进度条:叠加在屏幕中央,不破坏原布局 --> <ProgressBar android:id="@+id/progress_bar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" android:elevation="10dp" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.core.widget.NestedScrollView> 我希望地图只用显示一部分,宽度占满高度缩短。其实在地图没加载出来之前这个大小是正常的,加载出来之后我得拖动很久很久才能划到底下找到item
最新发布
11-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值