Android开发中布局与组件(二)—— padding 与 margin 的区别

android_中_padding与margin 立即下载

在 Android开发中我们会设置某个视图相对于别的视图的距离,这时我们就要用到 margin 和 padding ,但是有时候很容易把这两个属性弄混淆,那我们就看看他们的区别。

  • 外边距(margin): 属于布局参数,决定两个组件之间的距离。作用于多个组件之间。
  • 内边距(padding):不属于布局参数,这个参数是为了告诉组件在绘制自己的时候应该比自己的内容(content)大多少。作用于组件自己。

具体情况如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffca">

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:text="text1"
        android:textSize="30sp"
        android:background="#ff0000"/>

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/text1"
        android:layout_margin="8dp"
        android:padding="8dp"
        android:text="text2"
        android:textSize="30sp"
        android:background="#0000ff"/>

</RelativeLayout>

效果图如下:

在我们代码中定义的两个 TextView 中唯一的区别是 text2 比 text1 多了一个 android:padding="8dp" 属性,从而使他们的表现出来的样子大不相同,在 text2 和 text1 中同时定义的 android:layout_margin="8dp" 属性是让父视图在布置他们的位置是注意保持他们周围的边距为 8dp ,即 text1 距屏幕顶部的距离是 8dp ,距屏幕左边也是 8dp , text2 也是同样的,所以就导致 text1 底部和 text2 顶部的距离就是 16dp。
text2 由于有 android:padding="8dp" 的属性,所以就导致他的实际大小要比 text1 要大,如果我们把 text1 放在 text2 上边,就会发现 text1 的上下左右四个边 距 text2 的四个边的距离都是 8dp ,这就是 padding 属性的作用,它使得组件在保证内容不变的情况下增加组件的大小。

希望可以帮到你~

PS:开发了一个制作个性二维码的应用,有兴趣的朋友可以试一试~ 创意二维码制作

JPA - @MappedSuperclass - 基类注解 目录前言代码示例 前言 @MappedSuperclass注解一般作为基类的注解,表眀该类是映射的超类,继承于它的子类实体在映射时会自动添加到子类实体的对应数据库表中,用于共同的属性代码复用和模型分离。 代码示例 BaseEntity.java import lombok.Getter; import lombok.Setter; import javax.persistence.MappedSuperclass; import java.util.Date; @Getter @Setter @Ma 阅读详情

相关推荐

从零开始:使用VMOS Pro和Xposed绕过APP环境检测的完整指南

本文详细解析了使用VMOS Pro和Xposed框架绕过APP环境检测的完整技术方案,包括模拟器检测、Root环境检测和Hook框架检测的对抗方法。通过实战案例和代码示例,展示了如何利用Xposed Hook技术拦截检测逻辑,特别针对360加固等保护技术提供了解决方案。

ee345的博客 733

安卓底部弹窗

安卓底部弹窗

qq_41733851的博客 376

从BIOS到路由器:深入拆解SPI NOR Flash的硬件连接与‘芯片内执行’(XIP)奥秘

本文深入解析SPI NOR Flash的硬件连接与‘芯片内执行’(XIP)技术,从BIOS到路由器的应用场景出发,详细介绍了其架构精髓、典型连接方案、时序设计及性能优化方法。通过实际案例和参数对比,帮助开发者掌握NOR Flash在嵌入式系统中的关键作用与设计技巧,提升电路设计效率与可靠性。

weixin_30522183的博客 490

Android 内外边距 Padding 和 Margin 区别

如下设置了两个 TextView 控件,然后我们设置了其外边距以及内边距的大小,如果两个控件都设置了外边距,那么它们之间的距离就是两个边距之和。android : padding : 为内边框,指该控件内部内容,如文本/图片距离该控件的边距。控件中的内容与控件之间的距离称为内边距 ----- padding 即填充;控件和控件之间的距离称为外边距 ----- margin 即边界;其针对的是容器中的组件,即设置组件与父容器之间的边距(偏移);其针对的是组件中的元素,即设置组件内部元素之间的边距;...

低调,谦虚,自律,反思,成长! 4004

Keil 丢失编译器版本5、内核文件core_cm3.c报错

若用的是旧版本的core_cm3.c ,就使用编译器v5编译工程;若想用编译器v6,就更新4个相关的内核文件:(CMSIS:Cortex Microcontroller Software Interface Standard,ARM Cortex™ 微控制器软件接口标准)

qq_52804040的博客 6238

STM32--LoRa通信模块

模块根据MD0的配置与AUX引脚的状态会进入不同的功能其中在通信功能下,包含4种工作模式。

qq_47258284的博客 1万+

android中的padding,Android中的padding和margin的区别

在Android的布局中,常常有人将padding和margin搞混,他们其实不一样的,padding是该控件的内部距离。magin是该控件与其他控件之间的距离。例如android:layout_width="match_parent"android:layout_height="match_parent"android:background="@android:color/holo_blue_l...

weixin_34440189的博客 228

ChromeOS安装教程

ChromeOS,是基于Linux内核研发的ChromiumOS开源系统的闭源系统,最大的特色就是内置了Chrome浏览器,早期主要以Web云端应用程序为主,后来逐渐引入了Android子系统和Linux子系统。本文主要简述摸索的家用笔记本安装ChromeOS安装方法,供各位玩机朋友参考。

weixin_45751495的博客 8109

Android布局动画之animateLayoutChanges与LayoutTransition

Android的布局动画 你所应该了解的一切   关于布局动画是针对ViewGroup而言的,意指ViewGroup在增加子View或者删除子View时其子View的过渡动画,在android官网有这么一个简单的例子,其效果如下,接下来我们就通过这个例子来入门 ![](http://img.blog.youkuaiyun.com/20160916212650776 ) 最简单的布局动画实现

zejian的博客 3万+

PyTorch 面试题及详细答案120题(54-70)-- 模型训练与评估

本文介绍了PyTorch模型训练的基本流程和核心概念。主要内容包括: 模型训练基本流程:通过代码示例展示了数据准备、模型定义、损失函数和优化器选择、训练循环(前向传播、反向传播、参数更新)的完整过程,并包含验证集评估的实现。 训练过程中的关键概念: Epoch:完整遍历一次训练集 Batch:将数据分成小批次 Iteration:一个Batch的参数更新过程 三者关系:1 Epoch = N/B Batch = N/B Iteration(N为样本数,B为batch_size) PyTorch数据加载机制:

大剑师兰特的GIS世界 488

Mstar的Monitor方案OSD 菜单制作(五)——icon绘制

Mstar的Monitor方案(五)————绘制icon

android 系统定制开发那些事 1807

android constraintlayout垂直居中_Android Material Design控件使用——ConstraintLayout 约束布局

本章节将分为两个模块给大家推送,觉得不错的可以关注下哦介绍Android ConstraintLayout是谷歌推出替代PrecentLayout的组件。支持相对布局、线性布局、帧布局,看来更像是FrameLayout 、LinearLayout、`RelativeLayout·三者的结合体,并且比这三者更强大的是实现了百分比布局。大家都知道安卓碎片严重,使用百分比适配,那么将彻底解决适配问题总结...

weixin_39645308的博客 1039

“软开关”硬技术!ZVS和ZCS到底谁更牛?看完这个例子你就懂了!

❌ ZVS 和 ZCS 是万能的?→ 不,它们都有实现条件,谐振参数选不好照样硬拐弯!❌ 只要有谐振就一定ZVS?→ 不,一定要设计合适的死区时间和负载条件才行!ZVS 是让电压先“冷静”,ZCS 是让电流先“退场”。一个是开门不撞墙,一个是关门不甩门。懂了它们,功率设计就是在跳华尔兹而不是打架!💃🔌。

weixin_43199439的博客 1234

padding与margin的区别

<br />之前一直没有搞懂android:padding和android:layout_margin的区别,其实概念很简单,padding是站在父view的角度描述问题,它规定它里面的内容必须与这个父view边界的距离。margin则是站在自己的角度描述问题,规定自己和其他(上下左右)的view之间的距离,如果同一级只有一个view,那么它的效果基本上就和padding一样了。例如我的XML layout代码如下:<br /><?xml version="1.0" encoding="utf-8"?>

Lily's Android tech Blog 5万+

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Android布局中margin,padding,align的区别

一、Margin的用法 android:layout_marginxxx(xxx指的是left,right,top,bottom等)的用法是指当前组件距离其父组件在XXX方向上的边的距离(边距) 例如: android:layout_marginleft=“30px” -------当前组件距离父组件左边边界30像素 二、Padding的用法 android:paddingxxx(xxx指的是...

柯小帅的博客 1977

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

Android布局中margin与padding的区别

我们知道Android开发不仅仅有代码的动态控制,而且还涉及到布局的静态搭建(xml)。几乎在每一个xml文件中,我们总会看到margin和padding这两个属性,今天让我们初步探究一下它们之间的区别。首先为了便于说明,我们先画一个三层布局,如下图。这里布局的意思是广义的,泛指View等等。padding的含义在日常英语中,padding的含义为“垫料”,“衬料”等。在Android布局中,pad

Xiao_Spring的博客 1万+

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

Android布局中margin,padding,align的用法和区别

                  Android布局中margin,padding,align的用法和区别     一、margin的用法           android : layout_marginxxx的用法是指当前组件距离其父组件在xxx方向上的边距                       比如说:android:layout_marginleft = "30px"      -...

scarecrow_fly的博客 2万+

android padding作用,android:padding和android:margin的区别

看了网上的类似博客,并没有给出确定的区别。现在具体分析一下padding和android:margin的区别首先看一张图:顾名思义。padding为内边距;margin为外边距。安卓的view是一块矩形区域,padding是内边距,就是view(里面的内容)永远都至少和边界有一段设定好的距离。margin是外边距,就是外面的view无法完全靠近这个view的边界,至少要间隔一段设置好的距离。我理解...

weixin_33612966的博客 1724

android布局之margin和padding属性

以前用wxPython做Python开发时一直没有搞清楚margin和padding的区别和用法,也没有去深究。但是这两个属性在图形布局时非常重要,下面结合例子说一下这两个属性的区别与用法。 下文中只设置了layout_margin和padding属性,View属性中还可以分别设置其right|left|top|bottom四个方向的值,如果只设置layout_margin或padding一个属

RioCoder的专栏 1967

android padding margin的区别,如何正确使用padding和margin

原标题:如何正确使用padding和margin前面两期我们学习了LinearLayout线性布局的方向、填充模型、权重和对齐,那么本期我们来学习LinearLayout线性布局的内边距和外边距。关于padding和margin,很多同学傻傻分不清,相信通过今天的学习可以正确使用padding和margin。 一、内边距padding默认情况下,组件相互之间是紧紧靠在一起的。但是有时候需要组件各边...

weixin_42502288的博客 851

Android | XML相对布局 | margin与padding的区别

概述 先来看一张图片: 可以看到margin叫做偏移,就是上下左右几个方向,各自偏移多少 padding叫做填充,指的是在组件内的上下左右四个方向,各自填充多少 效果图 代码 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/...

lesileqin的博客 771

Android 开发中Layout_Margin与padding的区别以及Layout_gravity与gravity的区别

Layout_Margin与padding的区别以及Layout_gravity与gravity的区别 平时开发中这几个属性是我们经常使用的几个属性,偶尔脑子一糊涂,就容易弄混这些属性,下面,我就仔细介绍一下这几个属性: 1.首先介绍Layout_Margin与padding: 1.1.不设置任何Layout_Margin或者padding属性 <LinearLayo

滴水穿石,贵于坚持! 1161

android padding 和 margin的区别

前面两期我们学习了LinearLayout线性布局的方向、填充模型、权重和对齐,那么本期我们来学习LinearLayout线性布局的内边距和外边距。 关于padding和margin,很多同学傻傻分不清,相信通过今天的学习可以正确使用。     一、内边距padding 默认情况下,组件相互之间是紧紧靠在一起的。但是有时候需要组件各边之间有一定的内边距,那就可以通过以下几个属性来设置,内...

weixin_40213204的博客 545

安卓开发中margin和padding的区别

实际上,在web开发中两者的区别也是这样。在 Android 开发中,

ZTBztb123456的博客 815

padding和layout_margin的区别

顾名思义,padding有填充物的意思,而layout_margin中,layout是布局的意思,margin有边距、间隙的意思。所以,和layout_gravity和gravity 的区别有所类似, layout_gravity:容器或组件自身在整个布局中的排列方式;gravity:容器中的组件在容器中的排列方式或是组件中的文本、图片等元素在组件中的排列方式。带有layout的layout_m

蹲在人生路上的那只猫 1560

Android 中padding和margin的区别

区别padding是在控件内部的margin是在控件外部的如图所示,红线区域是margin,黑线区域是padding下面是TextView的各种 padding,margin的使用效果这里宽高全是wrap_content<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout

初心 5223
上一篇: Android开发中布局与组件(一)—— 屏幕尺寸单位dp,px,sp的探究
下一篇: Android开发中属性动画(ObjectAnimator)中 插值器(Time Interpolator )详解
ToBeTheOnlyOne
博客等级 码龄10年 9粉丝 34原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值