Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

用 Java 程式碼在 Android 中動態建立版面配置

編輯:關於 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」兩個常見技巧的結合應用。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved