<TextView
ndroid:text="@+id/TextView01"
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);
下面是一个实例来演示一下
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 = "http://www.51kdl.com/images/logo2.gif";
/** Called when the activity is first created. */
@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
{
/* new URL对象将网址传入 */
imageUrl = new URL(uriPic);
} catch (MalformedURLException e)
{
e.printStackTrace();
}
try
{
/* 取得联机 */
HttpURLConnection conn = (HttpURLConnection) imageUrl
.openConnection();
conn.connect();
/* 取得回传的InputStream */
InputStream is = conn.getInputStream();
/* 将InputStream变成Bitmap */
bitmap = BitmapFactory.decodeStream(is);
/* 关闭InputStream */
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();
}
};
}
其中在Androidmainfest.xml中要插入:< uses- permission android:name="android.permission.INTERNET"></uses-permission>