android:padding和android:margin的区别

本文详细解释了Android中Margin和Padding的概念及应用,包括它们如何影响控件布局和触摸响应。此外,还介绍了Android支持的各种长度单位及其应用场景。

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

本文综合了:http://zhujiao.iteye.com/blog/1856980 和 http://blog.youkuaiyun.com/maikol/article/details/6048647 两篇文章

加入了自己的一些心得。

Android的Margin和Padding跟Html的是一样的。如下图所示:黄色部分为Padding,灰色部分为Margin。

image

通俗的理解:

Padding 为内边框,指该控件内部内容,如文本/图片距离该控件的边距

Margin 为外边框,指该控件距离边父控件的边距

对应的属性为

android:layout_marginBottom="25dip" 
android:layout_marginLeft="10dip" 
android:layout_marginTop="10dip" 
android:layout_marginRight="10dip" 
android:paddingLeft="1dip" 
android:paddingTop="1dip" 
android:paddingRight="1dip" 
android:paddingBottom="1dip"

如果左右上下都是相同的设置则可以直接设置

android:layout_margin="10dip" 
android:padding="5dip"

当按钮分别设置以上两个属性时,得到的效果是不一样的。

android:paddingLeft="30px":

按钮上设置的内容(例如图片)离按钮左边边界30个像素。

android:layout_marginLeft="30px"

整个按钮离左边设置的内容30个像素

这二个属性是相对的,假设B是A的子控件,设置B的margin和设置A的padding能达到相同的效果。

设置padding的好处:

     如果imageview对应的图片比较小,点击不容易点中,通过增加padding可以增大点触敏感度


Android支持的长度单位。

  • px(像素):屏幕上的点。 
    pixels(像素). 不同设备显示效果相同,一般我们HVGA代表320x480像素,这个用的比较多。
  • in(英寸):长度单位。
  • mm(毫米):长度单位。
  • pt(磅):1/72英寸。 
    point,是一个标准的长度单位,1pt=1/72英寸,用于印刷业,非常简单易用;
  • dp(与密度无关的像素):一种基于屏幕密度的抽象单位。在每英寸160点的显示器上,1dp = 1px。
  • dip:与dp相同,多用于android/ophone示例中。 
    device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA、HVGA和QVGA 推荐使用这个,不依赖像素。
  • sp(与刻度无关的像素):与dp类似,但是可以根据用户的字体大小首选项进行缩放。 
    scaled pixels(放大像素). 主要用于字体显示best for textsize。

为了使用户界面能够在现在和将来的显示器类型上正常显示,建议大家始终使用sp作为文字大小的单位,Android默认的字号也是用的sp。

将dip作为其他元素的单位,比如长度、高度。当然,也可以考虑使用矢量图形,而不是用位图。

 

dp是与密度无关,sp除了与密度无关外,还与scale无关。

如果屏幕密度为160,这时dp和sp和px是一样的。1dp=1sp=1px,但如果使用px作单位,如果屏幕大小不变(假设还是3.2寸),而屏幕密度变成了320。

那么原来TextView的宽度设成160px,在密度为320的3.2寸屏幕里看要比在密度为160的3.2寸屏幕上看短了一半。

但如果设置成160dp或160sp的话。系统会自动将width属性值设置成320px的。

也就是160 * 320 / 160。其中320 / 160可称为密度比例因子。也就是说,如果使用dp和sp,系统会根据屏幕密度的变化自动进行转换.


<?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"> <TableLayout android:id="@+id/orderTable" android:layout_width="match_parent" android:layout_height="wrap_content" android:stretchColumns="*" android:shrinkColumns="*"> <!-- 表格标题行 --> <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> </androidx.core.widget.NestedScrollView> 填入信息后会变形,要求不能变型可以左右滑动,添加一个操作按钮
06-08
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/gradient_background"> <!-- 标题栏 --> <RelativeLayout android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?android:attr/actionBarSize" android:background="#44bd32"> <Button android:id="@+id/fanHui" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" android:background="@android:color/transparent" android:foreground="?android:attr/selectableItemBackground" android:text="返回" android:textColor="@android:color/white" android:textSize="16sp" /> </RelativeLayout> <!-- 底部按钮 --> <LinearLayout android:id="@+id/footer_buttons" android:layout_width="match_parent" android:layout_height="1dp" android:layout_alignParentBottom="true" android:background="@android:color/white" android:orientation="horizontal"> <!-- <Button--> <!-- android:id="@+id/device_location"--> <!-- android:layout_width="match_parent"--> <!-- android:layout_height="match_parent"--> <!-- android:gravity="center"--> <!-- android:text="设备定位"--> <!-- android:textSize="19sp"--> <!-- android:background="#44bd32"--> <!-- android:textColor="@android:color/white" />--> </LinearLayout> <RelativeLayout android:id="@+id/video_container" android:layout_width="match_parent" android:layout_height="380dp" android:layout_above="@id/footer_buttons" android:layout_below="@id/toolbar" android:layout_marginBottom="298dp" android:background="@android:color/black" android:padding="0dp"> <!-- 视频播放器 --> <VideoView android:id="@+id/video_player" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" /> <!-- 播放控制条 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="#99000000" android:orientation="horizontal" android:padding="8dp"> <ImageButton android:id="@+id/btn_play_pause" android:layout_width="40dp" android:layout_height="40dp" android:background="?attr/selectableItemBackgroundBorderless" android:src="@drawable/ic_play_sel" /> <SeekBar android:id="@+id/video_seekbar" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="center_vertical" /> <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="00:00/00:00" android:textColor="@android:color/white" android:textSize="14sp" /> </LinearLayout> <GridLayout android:id="@+id/thumbnail_grid" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/footer_buttons" android:layout_below="@id/video_container" android:layout_marginBottom="300dp" android:background="@android:color/white" android:columnCount="4" android:orientation="horizontal" android:padding="8dp" android:rowCount="2"> <!-- 动态添加的缩略图将放在这里 --> <!-- 示例缩略图 (可选) --> <ImageView android:layout_width="50dp" android:layout_height="wrap_content" android:layout_margin="4dp" android:adjustViewBounds="true" android:contentDescription="萤石缩略图" android:src="@drawable/default_thumbnail" /> <ImageView android:layout_width="50dp" android:layout_height="wrap_content" android:layout_margin="4dp" android:adjustViewBounds="true" android:contentDescription="萤石缩略图" android:src="@drawable/default_thumbnail" /> <!-- 更多缩略图... --> </GridLayout> </RelativeLayout> </RelativeLayout> 把布局文件中android:id="@+id/thumbnail_grid"放置在播放控制条的下面
最新发布
06-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值