Android零基礎入門第27節:正確使用padding和margin

原文: Android零基礎入門第27節:正確使用padding和margin

前面兩期咱們學習了LinearLayout線性佈局的方向、填充模型、權重和對齊,那麼本期咱們來學習LinearLayout線性佈局的內邊距和外邊距。html

關於padding和margin,不少同窗傻傻分不清,相信經過今天的學習能夠正確使用。android

 

 

1、內邊距padding

默認狀況下,組件相互之間是牢牢靠在一塊兒的。可是有時候須要組件各邊之間有必定的內邊距,那就能夠經過如下幾個屬性來設置,內邊距的值是具體的尺寸,如5dp。c#

  • android:padding:爲組件的四邊設置相同的內邊距。微信

  • android:paddingLeft:爲組件的左邊設置內邊距。架構

  • android:paddingRight:爲組件的右邊設置內邊距。app

  • android:paddingTop:爲組件的上邊設置內邊距。佈局

  • android:paddingBottom:爲組件的下邊設置內邊距。學習

內邊距的原理以下圖所示:優化

接下來經過一個簡單的示例程序來學習android:padding的使用用法。spa

繼續使用app/main/res/layout/目錄下的activity_main.xml文件,在其中填充以下代碼片斷:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="padding"
        android:padding="20dp"
        android:background="#00ffff"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="normal"
        android:background="#caa926"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="padding"
        android:paddingLeft="50dp"
        android:paddingRight="50dp"
        android:paddingTop="50dp"
        android:paddingBottom="50dp"
        android:background="#00f05f"/>
</LinearLayout>

運行程序,能夠看到下圖所示界面效果:

 

 

2、外邊距margin

 

經過設置內邊距,只能設置內容相對於組件之間的距離,而組件之間仍然是相鄰挨着的。在實際開發中,有時候須要組件之間有必定的間隔距離,那麼就須要用到外邊距了,能夠經過如下幾個屬性來設置。

  • android:layout_margin:本組件離上下左右各組件的外邊距。

  • android:layout_marginStart:本組件離開始的位置的外邊距。

  • android:layout_marginEnd:本組件離結束位置的外邊距。

  • android:layout_marginBottom:本組件離下部組件的外邊距。

  • android:layout_marginTop:本組件離上部組件的外邊距。

  • android:layout_marginLeft:本組件離左部組件的外邊距。

  • android:layout_marginRight:本組件離右部組件的外邊距。

外邊距的原理以下圖所示:

接下來經過一個簡單的示例程序來學習android:layout_margin的使用用法。

將上面的示例程序的佈局文件修改一下,以下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="margin 20dp"
        android:layout_margin="20dp"
        android:background="#00ffff"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="normal"
        android:background="#caa926"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="margin 50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:layout_marginTop="50dp"
        android:layout_marginBottom="50dp"
        android:background="#00f05f"/>
</LinearLayout>

從新運行程序,能夠看到下圖所示界面效果:

到此,關於LinearLayout線性佈局的內邊距和外邊距已經學習完成,你都掌握了嗎?padding和margin的區別是什麼?

若是把佈局的內邊距和外邊距放在一張圖中比較會更加直觀,以下圖所示:

 

也有這種說法:margin表明的是偏移,padding表明的是填充。固然,你也能夠根據本身的理解來總結。

 

 

 

今天就先到這裏,若是有問題歡迎留言一塊兒探討,也歡迎加入Android零基礎入門技術討論微信羣,共同成長!

此文章版權爲微信公衆號分享達人秀(ShareExpert)——鑫鱻全部,若轉載請備註出處,特此聲明!

 

往期總結分享:

Android零基礎入門第1節:Android的前世此生

Android零基礎入門第2節:Android 系統架構和應用組件那些事

Android零基礎入門第3節:帶你一塊兒來聊一聊Android開發環境

Android零基礎入門第4節:正確安裝和配置JDK, 高富帥養成第一招

Android零基礎入門第5節:善用ADT Bundle, 輕鬆邂逅女神

Android零基礎入門第6節:配置優化SDK Manager, 正式約會女神

Android零基礎入門第7節:搞定Android模擬器,開啓甜蜜之旅

Android零基礎入門第8節:HelloWorld,個人第一趟旅程出發點

Android零基礎入門第9節:Android應用實戰,不懂代碼也能夠開發

Android零基礎入門第10節:開發IDE大升級,終於迎來了Android Studio

Android零基礎入門第11節:簡單幾步帶你飛,運行Android Studio工程

Android零基礎入門第12節:熟悉Android Studio界面,開始裝逼賣萌

Android零基礎入門第13節:Android Studio配置優化,打造開發利器

Android零基礎入門第14節:使用高速Genymotion,跨入火箭時代

Android零基礎入門第15節:掌握Android Studio項目結構,揚帆起航

Android零基礎入門第16節:Android用戶界面開發概述

Android零基礎入門第17節:TextView屬性和方法大全

Android零基礎入門第18節:EditText的屬性和使用方法

Android零基礎入門第19節:Button使用詳解

Android零基礎入門第20節:CheckBox和RadioButton使用大全

Android零基礎入門第21節:ToggleButton和Switch使用大全

Android零基礎入門第22節:ImageView的屬性和方法大全

Android零基礎入門第23節:ImageButton和ZoomButton使用大全

Android零基礎入門第24節:自定義View簡單使用,打造屬於你的控件

Android零基礎入門第25節:簡單且最經常使用的LinearLayout線性佈局

Android零基礎入門第26節:兩種對齊方式,layout_gravity和gravity大不一樣

相關文章
相關標籤/搜索