2012年1月24日星期二

通過XML文件, 創建片段(Fragment)

前文"Android 3.0 新增的片段(Fragment)"的MyFragment.java類使用Java程序碼創建片段(Fragment). 本例修改MyFragment.java, 使用inflater類的inflate()方法, 通過XML文件, 創建片段(Fragment).

通過XML文件, 創建片段(Fragment)

- 建立新的XML文件, /res/layout/fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="FRAGMENT HERE" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
</LinearLayout>


- 修改MyFragment.java
package com.android3Frag;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class MyFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1, container, false);

return view;
}

}


- 沿用前文的main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<TextView
android:layout_width="0px"
android:layout_weight="2"
android:layout_height="match_parent"
android:text="@string/hello"/>
<fragment
class="com.android3Frag.MyFragment"
android:id="@+id/myfragment"
android:layout_width="0px"
android:layout_weight="3"
android:layout_height="match_parent"/>

</LinearLayout>


2012年1月16日星期一

Android 3.0 新增的片段(Fragment)

Android 3.0 新增片段(Fragment), 可以理解為類似嵌入活動(activity)內的一個子活動.

這是一個簡單的例子:

Android 3.0 新增的片段(Fragment)

在Eclipse創建一個新項目, Android3Frag; 要注意是: 建設目標(Build Targe)必須是 Android 3.0 (API Level 11)或更高.

先通過擴展 Fragment 創建一個 MyFragment 類, 並且重寫onCreateView()方法. 這便是我們的片段(Fragment).
package com.android3Frag;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MyFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Context context = getActivity().getApplicationContext();
LinearLayout layout = new LinearLayout(context);
TextView text = new TextView(context);
text.setText("FRAGMENT HERE");

ImageView image = new ImageView(context);
image.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));

layout.addView(text);
layout.addView(image);

return layout;
}

}


修改佈局, main.xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<TextView
android:layout_width="0px"
android:layout_weight="2"
android:layout_height="match_parent"
android:text="@string/hello"/>
<fragment
class="com.android3Frag.MyFragment"
android:id="@+id/myfragment"
android:layout_width="0px"
android:layout_weight="3"
android:layout_height="match_parent"/>

</LinearLayout>



相關文章:
- 通過XML文件, 創建片段(Fragment)

2012年1月13日星期五

谷歌發布Android設計網站 (Android Design)

Android設計網站 (Android Design)
谷歌推出Android設計網站(Android Design), 幫助學習創建Android用戶界面的設計原則, 構建組件塊, 以及設計模式. 無論你是UI專業人才或是開發用戶界面的開發人員,這些文件告訴你如何使良好的設計決策。

詳細參考: Android Developers Blog - Introducing the Android Design site

2012年1月11日星期三

2011年12月23日星期五

quiz-and-poll (測驗及投票)谷歌開放源代碼項目

quiz-and-poll
利用 Quiz & Poll, 教師及教育工作者可以使用Google Spreadsheets輕易創建測驗和調查的應用程序。 應用程序 Quizzes and Polls 通過 Android App 或嵌入 WebPlayer 分發給學生。 電子表格也包含學生的反應和排序,可用於評估統計學生。

Quiz & Poll是項谷歌開放源代碼項目, 有興趣可以參考: http://code.google.com/p/quiz-and-poll/



2011年12月17日星期六

Android 4.0.3


Galaxy Nexus 才剛推出市場, Android 4 已經成為舊版本!

Google 剛發佈 Android 4.0.3 - 新版本包括了各種對應手機和平板電腦的優化和bug修復, 以及為開發人員提供一些新增的應用程序接口. 新的API級別為15.

Android 4.0.3 將成為冰淇淋三明治(Ice Cream Sandwich)的基礎版本.

此外, Google 同時更新了對應的 SDK Tools (r16), Eclipse 插件 (ADT 16.0.1) 以及 NDK r7.

詳見: Android 開發者博客 - Android 4.0.3 Platform and Updated SDK tools.



2011年12月16日星期五

Android Training (線上 Android 訓練班)


谷歌宣布推出 Android Training 線上訓練班, 一個幫助 Android 開發人員打造更好的 Android 應用程序的線上訓練班.

這些訓練班旨在演示用於解決常見的Android開發問題的最佳做法。每個單元說明要解決一個問題,或實現一個功能的步驟,為開發人員提供豐富實用的代碼片段和示例.


信息來源: Android開發者博客 - Introducing Android Training