歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android基礎教程:在程序中添加圖片

Android基礎教程:在程序中添加圖片

日期:2017/3/1 10:52:39   编辑:Linux編程

第一種在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. }
Copyright © Linux教程網 All Rights Reserved