編輯:初階開發
近來因為專案需求,重寫了不少 Android 控件。寫到最後會發現目標其實就那幾個字:高效、輕巧、清晰、標準化。以下整理動態載入 Layout 的兩種做法,可依專案風格挑選。
(本文原轉自 https://blog.csdn.net/lzx_bupt/archive/2010/05/17/5600187.aspx )
這裡以線性版面為例,裡面放兩個按鈕:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/menubar"
android:background="@drawable/menubar"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton android:id="@+id/button1"
android:src="@drawable/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton android:id="@+id/button2"
android:src="@drawable/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
關鍵在於主版面要替子版面預留一個容器 box,並在這裡指定它的位置:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background">
<!-- 這個容器就是子 Layout 的落腳處,位置在此指定 -->
<LinearLayout android:id="@+id/box"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
</LinearLayout>
</RelativeLayout>
public class BackgroundTest extends Activity {
// 子 Layout 要以 View 形式加入主 Layout
private View mBarView;
// 主 Layout 中負責承載的容器
private LinearLayout mLinearLayout;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // 顯示主 Layout
mBarView = View.inflate(this, R.layout.main2, null); // 載入子 Layout
mLinearLayout = (LinearLayout) findViewById(R.id.box); // 取得容器
mLinearLayout.addView(mBarView); // 掛上 View
}
}
public class MenuLandscapeLinearLayout extends LinearLayout {
public MenuLandscapeLinearLayout(Context context) {
super(context);
// 在這裡載入需要的屬性,重用方法一的子 Layout
((Activity) getContext()).getLayoutInflater().inflate(R.layout.main2, this);
// 後續可以在此封裝各種自訂方法
}
}
public class BackgroundTest extends Activity {
private LinearLayout mLinearLayout;
// 宣告子 Layout 的 View 物件
private MenuLandscapeLinearLayout mMenuLandscapeLinearLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // 載入主 Layout
mLinearLayout = (LinearLayout) findViewById(R.id.box); // 找到容器
mMenuLandscapeLinearLayout = new MenuLandscapeLinearLayout(this); // 實例化子 View
mLinearLayout.addView(mMenuLandscapeLinearLayout); // 加入容器
}
}
方法一直觀易懂,適合版面簡單的情況;方法二把子版面封裝成一個類別,能把常用事件、資源整理都收進去,複用性更高,也更容易維護。
實際上可思考的空間還很多:把重複的事件綁定、資源載入邏輯抽出來封裝,不僅能省下大量重複程式碼,也能讓版面結構更乾淨。這正是重寫控件時最值得投入的部分。
ActivityManager 與 Activity 的關係、狀態追蹤與啟動 resume 流程
先把視角拉回應用開發者最熟悉的那一層。我們日常所寫的 Activity,其實只是「外特性空間」裡的一個客戶端對象;站在 AMS(Activity Manager Serv
Android 開發講義(四):純委託寫法與 UID 權限機制
範例三:採單純委託 撰寫步驟 Step-1:建立 Android 專案 Px03。 Step-2:ac01 直接持有 mp3Player 物件,UI 結構與前兩版一致:
Android 開發工具巡禮:App Inventor
隨著 Android 設備的日益普及,為讓更多人參與到 Android 軟體開發中來,Google 官方推出了一款名為 App Inventor 的開發工具,讓用戶無需編
Android 讀取手機通訊錄
在 Android 中讀取通訊錄,主要是透過 ContentResolver 存取 Contacts 的內容提供者。應用程式可以查詢聯絡人資料,並將結果顯示在 ListV