編輯:關於 Android 開發
許多 Android 初學者常搞不清楚 android:padding 與 android:layout_margin 的差別,其實概念很簡單:
padding 是站在父 view 的角度描述問題,它規定了「內容」與「該 view 邊界」之間的距離。margin 則是站在自己的角度描述問題,規定了自己與其他(上下左右)view 之間的距離。若同一層級只有一個 view,那麼它的視覺效果基本上就和 padding 一樣了。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="10dip"
android:paddingBottom="10dip">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FF0000"
android:text="@string/hello"
android:paddingLeft="50dip"
android:paddingRight="50dip"
android:paddingTop="50dip"
android:paddingBottom="50dip"
android:layout_marginBottom="10dip" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FF0000"
android:text="@string/hello"
android:paddingLeft="50dip"
android:paddingRight="50dip"
android:paddingTop="50dip"
android:paddingBottom="50dip"
android:layout_marginBottom="10dip" />
</LinearLayout>
在上述例子中,父 LinearLayout 的 padding 決定了子元件與父容器邊界的距離;而每個 TextView 的 padding 決定文字與自身邊界的距離,layout_marginBottom 則決定各 TextView 彼此之間的間隔。實際運行後對比,就能清楚感受兩者的差異。
Android WindowManager 窗口管理服務實戰
Android WindowManager 窗口管理服務實戰WindowManager 是 Android 系統中負責窗口管理的底層服務。許多常見 UI 能力,如 Act
Android ListView 實作單選與多選投票列表
Android ListView 實作單選與多選投票列表在許多應用场景中,會需要提供一組選項讓使用者勾選,例如投票、問卷或偏好設定。本文以 ListView 為基礎,說明
用 RadioButton 快速實現圖文上下或左右布局
用 RadioButton 快速實現圖文上下或左右布局在 Android 開發中,經常會遇到圖片與文字組合展示的需求,例如圖片在上、文字在下,或者圖片在左、文字在右。通常
Thread 與 Handler 搭配實戰:徹底搞懂 Android 的消息循環機制
Android 為開發者準備了 Handler 與 Looper 這組搭檔,專門用來解決「不同線程之間怎麼溝通」這個老問題。最典型的場景是:子線程在背景下載圖片,下載完成