2012年3月15日星期四

實現透明活動(Transparent Activity)

要令應用程序的背景設定為實現, 可以修改AndroidManifest.xml, 添加
android:theme="@android:style/Theme.Translucent"

透明活動(Transparent Activity)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.AndroidProgressDialog"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".AndroidProgressDialog"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

2012年3月14日星期三

實現透明進度對話框(Transparent Progress Dialog)

通過自定義對話框, 修改佈局參數的 alpha 屬性, 可以實現透明的進度對話框(Transparent Progress Dialog).

透明進度對話框(Transparent Progress Dialog)

創建 /res/layout/transparentprogressdialog.xml 定義包含進度條(ProgressBar)的對話框.
<?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"
android:paddingLeft="100dp"
android:paddingRight="100dp"
android:paddingTop="10dp"
android:paddingBottom="10dp">

<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>


修改主活動, 實現自定義對話框.
package com.AndroidProgressDialog;

import android.app.Activity;
import android.app.Dialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

public class AndroidProgressDialog extends Activity {

static final int CUSTOM_PROGRESSDIALOG_ID = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonStart = (Button)findViewById(R.id.start);

buttonStart.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new asyncTaskUpdateProgress().execute();
}
});
}

@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
switch(id) {
case CUSTOM_PROGRESSDIALOG_ID:
dialog = new Dialog(AndroidProgressDialog.this);
dialog.setContentView(R.layout.transparentprogressdialog);
dialog.setTitle("ProgressDialog");

WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.alpha = 0.5f;
dialog.getWindow().setAttributes(params);

break;
}

return dialog;
}

public class asyncTaskUpdateProgress extends AsyncTask<Void, Integer, Void> {

int progress;
//ProgressDialog progressDialog;

@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
dismissDialog(CUSTOM_PROGRESSDIALOG_ID);
}

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
progress = 0;
showDialog(CUSTOM_PROGRESSDIALOG_ID);
}

@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
while(progress<100){
progress++;
SystemClock.sleep(20);
}

return null;

}
}
}


主佈局 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="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- Start -" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/ic_launcher"/>

</LinearLayout>


2012年3月13日星期二

實現透明的對話框

我們可以通過修改對話框佈局參數的 alpha 屬性, 實現透明的對話框.
WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.alpha = 0.6f;
dialog.getWindow().setAttributes(params);

透明的對話框

package com.TransparentDialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

public class TransparentDialogActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonStartDialog = (Button)findViewById(R.id.start);
buttonStartDialog.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
//Create AlertDialog
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(TransparentDialogActivity.this);
myAlertDialog.setTitle("--- Title ---");

myAlertDialog.setMessage("Alert Dialog Message");

myAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
//...
}});

myAlertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
//...
}});

AlertDialog dialog = myAlertDialog.show();

WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.alpha = 0.6f;
dialog.getWindow().setAttributes(params);
}});
}

}


<?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" >

<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- Start -" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/ic_launcher"/>

</LinearLayout>

2012年3月10日星期六

自定義佈局的列表片段(ListFragment)

本例子示範如何在列表片段(ListFragment)中顯示自定義的佈局.

自定義佈局的列表片段(ListFragment)

創建/res/layout/row.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="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/text1"
android:textSize="26dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/text2"
android:textSize="26dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>


修改MyListFragment.java, 創建和使用我們自定義的陣列適配器, MyCustomAdapter.
package com.Android3ListFragment;

import android.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class MyListFragment extends ListFragment {

class Month{
String chinese;
String english;

Month(String c, String e){
chinese = c;
english = e;
}

public String getChinese(){
return chinese;
}
public String getEnglish(){
return english;
}
}


Month[] months ={
new Month("一月", "January"),
new Month("二月", "February"),
new Month("三月", "March"),
new Month("四月", "April"),
new Month("五月", "May"),
new Month("六月", "June"),
new Month("七月", "July"),
new Month("八月", "August"),
new Month("九月", "September"),
new Month("十月", "October"),
new Month("十一月", "November"),
new Month("十二月", "December")};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.listfragment, container, false);
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setListAdapter(new MyCustomAdapter(
getActivity(),
R.layout.row,
months));
}

public class MyCustomAdapter extends ArrayAdapter<Month>{

public MyCustomAdapter(Context context, int textViewResourceId,
Month[] objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater=getActivity().getLayoutInflater();
View row=inflater.inflate(R.layout.row, parent, false);
TextView text1 = (TextView)row.findViewById(R.id.text1);
TextView text2 = (TextView)row.findViewById(R.id.text2);

text1.setText(months[position].getChinese());
text2.setText(months[position].getEnglish());

return row;
}

}

}


其他文件, Android3ListFragmentActivity.java(主Activity), main.xml(主layout), listfragment.xml, 參考前文"Android 3.0 的列表片段(ListFragment)".

2012年3月8日星期四

AIDE - Android Java IDE: 在Android上運行的Android開發工具


AIDE - Android Java IDE Android App是一個直接在Android設備上開發Android Apps的集成開發環境(IDE). 它支持完整的編輯 - 編譯 - 運行週期: 功能豐富的編輯器, 提供先進的功能, 如代碼完成(code completion), 實時錯誤檢查(real-time error checking), 重構和智能代碼導航和運行應用程序.

AIDE完全兼容的Eclipse項目.

Google play 連結>>>

AIDE在Galaxy S2上運行的示範視頻:




MIT App Inventor測試版

MIT App Inventor測試版
早前 Google 開發的 App Inventor 轉移到麻省理工(Massachusetts Institute of Technology, MIT)而停了一段時間. 現在正名為 MIT App Inventor, 是麻省理工學院移動學習中心(MIT Center for Mobile Learning)的一個項目. 剛剛發佈測試版.

連結: http://appinventor.mit.edu/

MIT App Inventor

2012年3月6日星期二

Android 3.0 的列表片段(ListFragment)

列表片段(ListFragment)
- 建立新的XML文件, /res/layout/listfragment.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"
android:background="#101010">

<ListView android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"/>
<TextView android:id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No data"/>
</LinearLayout>


- 擴展ListFragment, 建立新的MyListFragment.java類.
package com.Android3ListFragment;

import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

public class MyListFragment extends ListFragment {

String[] month ={
"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.listfragment, container, false);
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setListAdapter(new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_1,
month));
}

}


- 修改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_height="wrap_content"
android:layout_width="0px"
android:layout_weight="3"
android:text="@string/hello" />
<fragment
class="com.Android3ListFragment.MyListFragment"
android:id="@+id/mylistfragment"
android:layout_width="0px"
android:layout_weight="1"
android:layout_height="match_parent"/>

</LinearLayout>


主Activity使用自動生成的代碼便可以:
package com.Android3ListFragment;

import android.app.Activity;
import android.os.Bundle;

public class Android3ListFragmentActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}



相關文章:
- 自定義佈局的列表片段(ListFragment)