編輯:關於 Android 程式設計
一般來說,在 Android 中用 XML 做版面配置要方便又快速。但在某些特殊情況下,我們也可以用程式碼取代 XML 來達到相同的效果。以下先以 TextView 為例說明兩者的對應關係。
XML 寫法:
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
等價的程式碼寫法:
LinearLayout.LayoutParams param =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
final TextView tv = new TextView(this);
tv.setText("Hello Android!");
tv.setGravity(1);
tv.setId(TestView01);
layout.addView(tv,param);
setContentView(layout);
下面用一個完整實例來演示:以程式碼建立版面配置,並在子執行緒中定時從網路下載圖片顯示到 ImageView 上。
package AdView.AD.android;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class AdView extends Activity
{
private static final int myImageView1 = 0;
private ImageView mImageView1;
boolean run=true;
String uriPic = "https://up.2cto.com/2012/0816/20120816104135808.gif";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
ImageView iv= new ImageView(this);
iv.setId(myImageView1);
layout.addView(iv,params);
setContentView(layout);
mImageView1 = (ImageView) findViewById(myImageView1);
new Thread(mTasks).start();
}
public void onDestroy(){
run=false;
super.onDestroy();
}
public void getimage(){
mImageView1.setImageBitmap(getURLBitmap());
}
public Bitmap getURLBitmap()
{
URL imageUrl = null;
Bitmap bitmap = null;
try
{
imageUrl = new URL(uriPic);
} catch (MalformedURLException e)
{
e.printStackTrace();
}
try
{
HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e)
{
e.printStackTrace();
}
return bitmap;
}
private Runnable mTasks =new Runnable(){
public void run()
{
while(run){
try{
Thread.sleep(500);
mHandler.sendMessage(mHandler.obtainMessage());
}catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
};
Handler mHandler = new Handler(){
public void handleMessage(Message msg)
{
super.handleMessage(msg);
getimage();
}
};
}
最後別忘了在 AndroidManifest.xml 中加上網路存取權限:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
這個範例同時示範了「以程式碼建立版面配置」與「透過 Handler 在子執行緒下載圖片、回主執行緒更新 UI」兩個常見技巧的結合應用。
Android 實作:手機安全衛士(16)——打造多樣化的複雜 ListView
1 介紹 延續昨天的內容,今天我們繼續完善應用列表。應用可分為系統應用與使用者應用,安裝位置則分為手機內部儲存與 sdcard。因此,我們在 ListView 中加入一個
解決 Eclipse 開發 Android 時出現的 Cannot connect to VM 錯誤
在 Eclipse 中開發 Android,執行時偶爾會跳出這樣的錯誤訊息:Launching ???? has encountered a problem. Canno
Android 使用 PopupWindow 實作彈出視窗(可用於提示等)
效果圖如下: PopupWindow 是一個能顯示於當前 Activity 之上的浮動容器。它彈出的位置可以調整:依有無偏移量分為「無偏移」與「有偏移」兩種;依參照對象
基於 Google ZXing 實現二維碼掃描,並仿微信掃描動效
說起二維碼,很多人的記憶都始於微信。當年微信上線掃一掃,拿一張圖掃一下就能加好友,確實讓人覺得新鮮。那時候我對二維碼的認知還很模糊,後來項目裡要加掃描功能,上網一查,發現