Android 设置Padding和Margin(动态/静态)

本文详细介绍了Android界面开发中padding和margin的概念及其作用,通过实例展示了如何在XML布局文件中静态设置以及在代码中动态设置padding和margin。同时,提供了dp转px的方法,并解释了其在屏幕适配中的重要性。最后,演示了动态设置margin的代码实现。

一、什么是padding,什么是margin?

在Android界面开发时,为了布局更加合理好看,很多时候会用上Padding和Margin,

padding和margin是什么呢?即内边距和外边距;

某个View指定为padding是针对该View里面的子View距离该View距离而言的,或者是里面的内容距离容器的距离。

某个View指定为margin是针对该View本身距离别人或者父View而言的。

例如下图,输入框里面的文字内容,如果不设置内边距,那么就会紧挨左上角,这样看起来,就很不友好,合理的设置padding看起来会舒服很多。

如果,不设置外边距,会充满整个父布局,也不好看,这时候就需要margin属性(外边距)。

 类似于控件的基础属性,并且不会变化的,我们一般会直接在xml文件里直接设置,这是上图的布局代码

<androidx.appcompat.widget.AppCompatEditText
                android:id="@+id/chat_input_edit"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginStart="10dp"
                android:layout_marginTop="10dp"
                android:layout_marginEnd="10dp"
                android:layout_marginBottom="10dp"
                android:paddingHorizontal="12dp"
                android:paddingVertical="10dp"
                android:textColor="@color/white"
                android:textColorHint="#94ffffff"
                android:textSize="14sp" />

 二、动态设置边距

那么怎么动态设置padding和margin呢?其实也很简单。

1.设置padding

view.setPadding(int left, int top, int right, int bottom)//view为你要设置的控件

例子:在我点击搜索框后,搜索框获取焦点,准备输入内容的时候,图标消失,文本内边距修改,实现代码如下

editText.setOnFocusChangeListener { view, b ->
            if (b) {//使用dp2px方法进行屏幕适配
                view.setPadding(DPUtils.dp2px(12f),DPUtils.dp2px(6f),DPUtils.dp2px(12f),DPUtils.dp2px(6f))
                searchIcon.visibility = View.GONE
            }
        }

实现效果:最开始文本里左边内边距32dp,点击后变成12dp

//这是dp转为px的方法
private fun dp2px(i: Int): Int {
    return (Resources.getSystem().displayMetrics.density * i + 0.5f).toInt()} 

为什么会有dp2px这个方法来转一下呢?

android中px与sp,dp之间的转换_跑快点的博客-CSDN博客_android px sp由于Android手机厂商很多,导致了不同设备屏幕大小和分辨率都不一样,然而我们开发者要保持在不同设备上显示同样的视觉效果,就需要做一些适配效果。 相关名词解释 屏幕大小:通常指的是屏幕对角线的长度,使用“寸”为单位来衡量。 分辨率:指手机屏幕的像素点个数,例如:720*1280,指的是宽有720个像素点,高有1280个像素点。 dpi:指的是每英寸像素,是由对角线上的像素点数除以屏幕大小所https://blog.csdn.net/qidingquan/article/details/53714603?spm=1001.2101.3001.6650.4&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-4-53714603-blog-91974773.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-4-53714603-blog-91974773.pc_relevant_default&utm_relevant_index=92.动态设置margin

android的view中有setPadding,但是没有直接的setMargin方法。如果要在代码中设置该怎么做呢?可以通过设置view里面的 LayoutParams 设置,而这个LayoutParams是根据该view在不同的GroupView而不同的。这儿用的是RelativeLayout是因为在他的父布局是RelativeLayout哦,用成其他的会报错哦~~

val lp = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT)
lp.setMargins(0, 0, DPUtils.dp2px(7f), DPUtils.dp2px(7f))
//RelativeLayout可以通过LayoutParams的addRule来添加约束,其他的布局也有类似的一些方法
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)
lp.addRule(RelativeLayout.ALIGN_PARENT_END)
textView.layoutParams = lp

<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true"> <LinearLayout android:id="@+id/MainPage" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <RelativeLayout android:id="@+id/SerialContent" android:layout_width="match_parent" android:layout_height="60dp"> <TextView android:id="@+id/Serial" android:layout_width="160dp" android:layout_height="match_parent" android:background="@drawable/form_shape" android:gravity="center" android:text="串口号" /> <Spinner android:id="@+id/SerialName" android:layout_width="100dp" android:layout_height="80dp" android:gravity="center" android:spinnerMode="dropdown" android:layout_toRightOf="@+id/Serial"/> <Spinner android:id="@+id/BaudRate" android:layout_width="140dp" android:layout_height="80dp" android:layout_toRightOf="@id/SerialName" android:gravity="center" android:spinnerMode="dropdown" /> <Button android:id="@+id/OpenSerial" android:layout_width="120dp" android:layout_height="match_parent" android:layout_toRightOf="@id/BaudRate" android:text="打开串口" /> <Button android:id="@+id/QuitSoftware" android:layout_width="140dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:text="退出软件" /> </RelativeLayout> <FrameLayout android:id="@+id/previewContent" android:layout_width="604.7dp" android:layout_height="430dp" android:visibility="gone"> <androidx.camera.view.PreviewView android:id="@+id/preview" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout> <RelativeLayout android:id="@+id/handleContent" android:layout_width="match_parent" android:layout_height="430dp" android:visibility="visible"> <TextView android:id="@+id/handle" android:layout_width="160dp" android:layout_height="match_parent" android:background="@drawable/form_shape" android:gravity="center" android:text="手柄" /> <LinearLayout android:id="@+id/RightContent" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_toRightOf="@+id/handle" android:orientation="vertical"> <RelativeLayout android:id="@+id/content1" android:layout_width="match_parent" android:layout_height="60dp" android:layout_toRightOf="@+id/handle"> <Button android:id="@+id/ChangeDial" android:layout_width="140dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:text="带拨号盘版" /> </RelativeLayout> <LinearLayout android:id="@+id/DialContent" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <EditText android:id="@+id/receivedDataView" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@android:drawable/editbox_background" android:focusable="false" android:focusableInTouchMode="false" android:cursorVisible="false" android:gravity="top|left" android:inputType="textMultiLine" android:padding="8dp" android:scrollbars="vertical" android:hint="等待数据..." android:textSize="14sp" /> <LinearLayout android:id="@+id/mianBoard" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical"> <LinearLayout android:id="@+id/control" android:layout_width="match_parent" android:layout_height="74dp" android:orientation="horizontal" android:visibility="visible"> <EditText android:id="@+id/commandInput" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:hint="输入指令" android:paddingLeft="10dp" android:inputType="text" android:maxLines="1" /> <Button android:id="@+id/sendButton" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="发送" /> <Button android:id="@+id/clearDataView" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="清空" /> </LinearLayout> <GridLayout android:id="@+id/keyBoard" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="4" android:columnCount="3" android:rowCount="4" android:visibility="visible"> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="1" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="2" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="3" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="4" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="5" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="6" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="7" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="8" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="9" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="*" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="0" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="#" /> </GridLayout> <GridLayout android:id="@+id/NoKeyBoard" android:layout_width="match_parent" android:layout_height="0dp" android:columnCount="6" android:rowCount="4" android:layout_weight="5" android:visibility="gone"> <TextView android:id="@+id/keyA" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="KEYA" android:gravity="center"/> <Button android:id="@+id/keyAStart" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1.2" android:layout_rowWeight="1" android:text="开灯" /> <Button android:id="@+id/keyAClose" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1.2" android:layout_rowWeight="1" android:text="关灯" /> <TextView android:id="@+id/keyB" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="KEYB" android:gravity="center"/> <Button android:id="@+id/keyBStart" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1.2" android:layout_rowWeight="1" android:text="开灯" /> <Button android:id="@+id/keyBClose" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1.2" android:layout_rowWeight="1" android:text="关灯" /> <TextView android:id="@+id/key1" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="KEY1" android:gravity="center"/> <Button android:id="@+id/key1Start" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="开灯" /> <Button android:id="@+id/key1Close" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="关灯" /> <TextView android:id="@+id/key2" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="KEY2" android:gravity="center"/> <Button android:id="@+id/key2Start" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="开灯" /> <Button android:id="@+id/key2Close" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="关灯" /> <TextView android:id="@+id/key3" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="KEY3" android:gravity="center"/> <Button android:id="@+id/key3Start" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="开灯" /> <Button android:id="@+id/key3Close" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="关灯" /> <TextView android:id="@+id/key4" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="KEY4" android:gravity="center"/> <Button android:id="@+id/key4Start" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="开灯" /> <Button android:id="@+id/key4Close" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="关灯" /> <TextView android:id="@+id/key5" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="KEY5" android:gravity="center"/> <Button android:id="@+id/key5Start" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="开灯" /> <Button android:id="@+id/key5Close" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="关灯" /> </GridLayout> </LinearLayout> </LinearLayout> </LinearLayout> </RelativeLayout> <RelativeLayout android:id="@+id/VoiceContent" android:layout_width="match_parent" android:layout_height="80dp"> <TextView android:id="@+id/voice" android:layout_width="160dp" android:layout_height="match_parent" android:background="@drawable/form_shape" android:gravity="center" android:text="录音" /> <Button android:id="@+id/RecordingVoice" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:layout_toRightOf="@+id/voice" android:text="开始录音" /> <TextView android:id="@+id/ShowVoiceData" android:layout_width="150dp" android:layout_height="match_parent" android:layout_toRightOf="@+id/RecordingVoice" android:background="@drawable/solid_shape" android:gravity="center" android:ellipsize="end" android:singleLine="false" android:maxLines="3"/> <Button android:id="@+id/PlayVoice" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:layout_toRightOf="@id/ShowVoiceData" android:text="播放语音" /> <Button android:id="@+id/DeleteAudioAndView" android:layout_width="150dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_margin="2dp" android:text="删除" /> </RelativeLayout> <RelativeLayout android:id="@+id/CameraContent" android:layout_width="match_parent" android:layout_height="80dp"> <TextView android:id="@+id/camera" android:layout_width="160dp" android:layout_height="match_parent" android:background="@drawable/form_shape" android:gravity="center" android:text="摄像头" /> <Button android:id="@+id/OpenAndCloseCamera" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:layout_toRightOf="@+id/camera" android:text="打开摄像头" /> <Button android:id="@+id/photograph" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:layout_toRightOf="@id/OpenAndCloseCamera" android:text="拍照" /> <TextView android:id="@+id/PhotographData" android:layout_width="150dp" android:layout_height="match_parent" android:layout_toRightOf="@+id/photograph" android:background="@drawable/solid_shape" android:gravity="center" android:ellipsize="end" android:singleLine="false" android:maxLines="3"/> <Button android:id="@+id/RecordingView" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:layout_toRightOf="@+id/PhotographData" android:text="录制视频" /> <TextView android:id="@+id/RecordingViewData" android:layout_width="150dp" android:layout_height="match_parent" android:layout_toRightOf="@+id/RecordingView" android:background="@drawable/solid_shape" android:gravity="center" android:ellipsize="end" android:singleLine="false" android:maxLines="3"/> <Button android:id="@+id/DeleteView" android:layout_width="150dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_margin="2dp" android:text="删除" /> </RelativeLayout> <LinearLayout android:id="@+id/ScreenTestContent" android:layout_width="match_parent" android:layout_height="80dp"> <TextView android:id="@+id/ScreenTest" android:layout_width="160dp" android:layout_height="match_parent" android:background="@drawable/form_shape" android:gravity="center" android:text="屏幕测试" /> <Button android:id="@+id/ScreenCheck" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:text="屏幕检测" /> <TextView android:id="@+id/TouchScreenCheck" android:layout_width="150dp" android:layout_height="match_parent" android:background="@drawable/form_shape" android:gravity="center" android:text="触摸屏检测" /> <Button android:id="@+id/ScribingTest" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:text="划线测试" /> <Button android:id="@+id/SinglePointTest" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:text="单点测试" /> </LinearLayout> <LinearLayout android:id="@+id/NetPortContent" android:layout_width="match_parent" android:layout_height="80dp"> <TextView android:id="@+id/NetPort" android:layout_width="160dp" android:layout_height="match_parent" android:background="@drawable/form_shape" android:gravity="center" android:text="网口" /> <TextView android:id="@+id/NetPortData" android:layout_width="150dp" android:layout_height="match_parent" android:ellipsize="end" android:layout_margin="2dp" android:gravity="center" android:background="@drawable/solid_shape"/> <TextView android:id="@+id/ping" android:layout_width="150dp" android:layout_height="match_parent" android:background="@drawable/form_shape" android:gravity="center" android:text="ping" /> <EditText android:id="@+id/pingInput" android:layout_width="150dp" android:layout_height="match_parent" android:hint="请输入" android:paddingLeft="10dp" android:text="192.168.1.1" android:layout_margin="2dp" android:inputType="text" android:maxLines="1"/> <Button android:id="@+id/pingSend" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:text="发送ping"/> <ScrollView android:id="@+id/scrollView" android:layout_width="200dp" android:layout_height="match_parent" android:fillViewport="true"> <TextView android:id="@+id/pingData" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/solid_shape" android:textSize="14sp" android:singleLine="false" android:maxLines="100" android:scrollbars="vertical" android:layout_margin="2dp"/> </ScrollView> <Button android:id="@+id/pingStop" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:text="停止"/> </LinearLayout> <RelativeLayout android:id="@+id/USBContent" android:layout_width="match_parent" android:layout_height="80dp"> <TextView android:id="@+id/USB" android:layout_width="160dp" android:layout_height="match_parent" android:background="@drawable/form_shape" android:gravity="center" android:text="USB接口" /> <TextView android:id="@+id/USBPath" android:layout_width="150dp" android:layout_height="match_parent" android:hint="未连接" android:ellipsize="end" android:layout_margin="2dp" android:gravity="center" android:background="@drawable/solid_shape" android:layout_toRightOf="@+id/USB"/> <Button android:id="@+id/getUSBPath" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:text="获取USB路径" android:layout_toRightOf="@+id/USBPath"/> <EditText android:id="@+id/WriteTextData" android:layout_width="150dp" android:layout_height="match_parent" android:inputType="text" android:paddingLeft="10dp" android:layout_toRightOf="@+id/getUSBPath"/> <Button android:id="@+id/WriteText" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:text="写入文本" android:layout_toRightOf="@+id/WriteTextData"/> <ScrollView android:id="@+id/scrollViewOpenTextData" android:layout_width="200dp" android:layout_height="match_parent" android:fillViewport="true" android:layout_toRightOf="@+id/WriteText"> <TextView android:id="@+id/OpenTextData" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/solid_shape" android:textSize="14sp" android:singleLine="false" android:maxLines="100" android:scrollbars="vertical" android:layout_margin="2dp" android:gravity="center"/> </ScrollView> <Button android:id="@+id/OpenText" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:text="打开文本" android:layout_toRightOf="@+id/scrollViewOpenTextData"/> <Button android:id="@+id/DeleteText" android:layout_width="150dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_margin="2dp" android:text="删除" /> </RelativeLayout> </LinearLayout> </ScrollView>使以上代码实现自适应屏幕大小
最新发布
08-14
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

&岁月不待人&

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值