Android基础教程:在程序中添加图片


第一种在xml布局文件中添加(静态的添加) 

  1. <ImageView  
  2. Android:id="@+id/image_1"  
  3. android:layout_width="wrap_content"  
  4. android:layout_height="wrap_content"  
  5. android:src="@drawable/me"  
  6. ></ImageView>  

就像在html标签中

image

android:src引用的图片的路径!

在res建立drawable文件夹其中的me为文件的名字。

 

第二中(动态的添加)

在Activity中动态的添加

例如当点击按钮时 显示图片

  1. public class Framemain extends Activity {  
  2.     private ImageView imageView;  
  3.     public void onCreate(Bundle savedInstanceState) {  
  4.         super.onCreate(savedInstanceState);  
  5.         setContentView(R.layout.framemain);  
  6.         Intent intent=this.getIntent();  
  7.         TextView textView=(TextView)findViewById(R.id.showUser);  
  8.         textView.setText(intent.getStringExtra("username"));      
  9.         imageView=(ImageView) findViewById(R.id.me_image);  
  10.         Resources resources=getResources();  
  11.         Drawable drawable =resources.getDrawable(R.drawable.me);  
  12.         imageView.setImageDrawable(drawable);  
  13.     }  
  14. }  

相关内容