編輯:關於 Android 程式設計
聲明 layout 有兩種方式:
1. 在 XML 文件中聲明 UI 組件。這是推薦做法,結構清晰、易於預覽,也方便與設計稿對照。
2. 在運行時通過代碼實例化布局元素。以編碼方式創建 View 或 ViewGroup 對象,並操作它們的屬性。
兩者並非互斥。實戰中常見的組合是:主體布局寫在 XML 裡,運行時再動態往容器中 addView() 一些列表項、表單行。所以兩種方式都要掌握。
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
private LinearLayout linearLayout;
private TextView textView;
private Button button;
public static final int VERTICAL = 1;
public static final int MATCH_PARENT = -1;
public static final int WRAP_CONTENT = -2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 以編碼方式添加 layout
linearLayout = new LinearLayout(this);
// 設置 LinearLayout 方向:0 為水平,1 為垂直,默認水平
linearLayout.setOrientation(VERTICAL);
// 設置布局參數,-1 為 MATCH_PARENT,-2 為 WRAP_CONTENT
linearLayout.setLayoutParams(
new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
textView = new TextView(this);
textView.setText("ThisIsATextView");
textView.setBackgroundColor(Color.RED);
textView.setLayoutParams(
new ViewGroup.LayoutParams(MATCH_PARENT, WRAP_CONTENT));
button = new Button(this);
button.setText("ThisIsAButton");
button.setBackgroundColor(Color.GREEN);
button.setLayoutParams(
new ViewGroup.LayoutParams(MATCH_PARENT, WRAP_CONTENT));
linearLayout.addView(button);
linearLayout.addView(textView);
// 布局寫好後,別忘了添加到 Activity 中
setContentView(linearLayout);
}
}
這裡有兩個新手常犯的錯誤值得強調:
其一,忘了 setContentView。 光把 View 對象 new 出來並組裝好是不夠的,必須交給 Activity 才會顯示。
其二,LayoutParams 用錯類型。 子 View 的 LayoutParams 必須與其父容器的 LayoutParams 類型匹配,否則會拋 ClassCastException。例如 LinearLayout 的子元素要用 LinearLayout.LayoutParams,才能設置 layout_weight。
每個 layout 文件必須包含一個確定的根元素,它必須是 View 或 ViewGroup 的對象。那這兩個類各自負責什麼?
View
為用戶界面組件提供基本的搭建區域。View 類是所有可視控件的老祖宗,widget 包下的 Button、TextView、ImageView 等都用來創建可交互的 UI 組件。同時 View 也是 ViewGroup 的父類。
ViewGroup
是各類 Layout 的父類,本質上是「不可見的容器」——它自己不顯示內容,負責容納其他 View 或 ViewGroup,並定義它們的擺放規則(布局屬性)。LinearLayout、RelativeLayout、FrameLayout、ConstraintLayout 都是它的子類。
構建界面層的思路,就是通過嵌套容器與添加子元素,一層層把視圖樹「長」出來。
<!-- 根元素 LinearLayout 是 ViewGroup 的子類 -->
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- 添加子組件來豐富視圖層 -->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f00"
android:layout_weight="1"
android:text="ThisIsAButton" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#0f0"
android:text="ThisIsATextView" />
</LinearLayout>
layout_width / layout_height
每個 View 都必須指定,取值有三種:match_parent(與父容器等寬/高)、wrap_content(包裹自身內容)、具體數值(如 48dp)。
orientation
LinearLayout 專屬,決定子元素排列方向。
layout_weight
LinearLayout 專屬,按權重比例分配剩餘空間。注意:權重是在測量完 wrap_content 部分之後,對剩餘空間做分配,所以搭配 layout_height="0dp" 使用性能更好,能避免多餘的二次測量。
padding 與 margin
padding 是容器內部的留白,算在自身尺寸裡;margin 是與兄弟節點的外間距,不算在自身尺寸裡。兩者含義不同,不要混用。
id
寫成 @+id/xxx 表示新建 id 資源,@id/xxx 表示引用已有的。代碼中通過 findViewById() 獲取。
視圖層嵌套越深,測量和布局的開銷越大(measure 與 layout 是遞歸的)。實踐中建議:
ConstraintLayout 一層搞定的,不要用三四層 LinearLayout 嵌套。<merge> 標簽消除引入布局時多餘的根節點。<include> 複用片段,用 <ViewStub> 延遲加載不常用的複雜視圖。下一篇再談談各類 Layout 的具體特性與選型。
Android 圖像處理:黑白濾鏡實作
黑白濾鏡是最經典的圖像效果之一。它會去除色彩資訊,只保留明暗層次,使照片呈現出紀錄片、街拍或復古影像般的氛圍。在 Android 中,這類效果同樣可以透過 Bitmap
NetworkOnMainThreadException 的成因與兩種解決方案
把 Android 4.0 之前的老項目搬到新版本上運行,網絡請求經常直接拋出 NetworkOnMainThreadException。這個異常並不是 bug,而是系統
OpenGL ES 多重紋理與程序紋理實作
本範例展示如何使用 OpenGL ES 建立星空、地球與月球的 3D 場景,並透過多重紋理與程序紋理呈現更豐富的視覺效果。範例中包含矩陣管理、天球生成、地球晝夜紋理切換與
Android 網絡開發實戰:Volley 入門與 Cloudant 通訊(一)
引言 網絡一直是我的知識盲點,前陣子我抽空系統學習了 Volley 這個網絡工具的使用,並進一步研讀了它的源碼,頗有心得,想在此分享。Android 平台上成熟的網絡庫並