Android中经常用到图片,比如图片浏览、图标等等,今天学习下image控件,image控件主要有ImageButton和ImageView两种。
(1)ImageButton
在布局文件增加:
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="21dp"
android:src="@drawable/ic_launcher" /> 效果:如果想改变按钮图案,可以在drawable下放自定义图标,然后修改imagebutton里面src代码即可,如图
(2)ImageView
这个稍微复杂点,但也不是很难,我们先随便添加几张图片如图:
简单修改下xml文件,
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.example.imagebutton.MainActivity" > 10 11 <LinearLayout 12 android:id="@+id/layout1" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:orientation="vertical"> 16 <TextView 17 android:id="@+id/textView1" 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 android:text="@string/hello_world" /> 21 22 <ImageView 23 android:id="@+id/imageView1" 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content" 26 android:layout_alignLeft="@+id/textView1" 27 android:layout_below="@+id/textView1" 28 android:layout_marginTop="18dp" 29 android:src="@drawable/a" /> 30 31 <RadioGroup 32 android:id="@+id/radioGroup1" 33 android:layout_width="wrap_content" 34 android:layout_height="wrap_content" 35 android:layout_alignLeft="@+id/spinner1" 36 android:layout_below="@+id/spinner1" 37 android:layout_marginTop="29dp" > 38 </RadioGroup> 39 40 </LinearLayout> 41 42 <Button 43 android:id="@+id/button1" 44 android:layout_width="wrap_content" 45 android:layout_height="wrap_content" 46 android:layout_alignLeft="@+id/layout1" 47 android:layout_below="@+id/layout1" 48 android:text="上一个" /> 49 50 <Button 51 android:id="@+id/button2" 52 android:layout_width="wrap_content" 53 android:layout_height="wrap_content" 54 android:layout_alignBottom="@+id/button1" 55 android:layout_centerHorizontal="true" 56 android:text="下一个" /> 57 58 </RelativeLayout>
接下来实现点击上一个或者下一个按钮,图片会发生改变功能,代码如下:
1 package com.example.imagebutton; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.Menu; 6 import android.view.MenuItem; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button; 10 import android.widget.ImageView; 11 12 public class MainActivity extends Activity { 13 14 private Button prebutton = null; 15 private Button nextbutton = null; 16 private ImageView imageview1 = null; 17 public int i = 0; 18 private int[ ] iImages = {R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e,R.drawable.f}; 19 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_main); 24 25 prebutton = (Button)findViewById(R.id.button1); 26 nextbutton = (Button)findViewById(R.id.button2); 27 imageview1 = (ImageView)findViewById(R.id.imageView1); 28 prebutton.setOnClickListener(new prebuttonclick()); 29 nextbutton.setOnClickListener(new nextbuttonclick()); 30 31 imageview1.setImageResource(i); 32 } 33 34 35 public class prebuttonclick implements OnClickListener { 36 37 @Override 38 public void onClick(View v) { 39 if (i > 0) 40 { 41 imageview1.setImageResource(iImages[--i]); 42 } 43 else if (i <= 0) 44 { 45 i = 5; 46 imageview1.setImageResource(iImages[i]); 47 } 48 } 49 } 50 51 52 public class nextbuttonclick implements OnClickListener { 53 @Override 54 public void onClick(View v) { 55 if (i < 5) 56 { 57 imageview1.setImageResource(iImages[++i]); 58 } 59 else if (i >= 5) 60 { 61 i = 0; 62 imageview1.setImageResource(iImages[i]); 63 } 64 65 } 66 } 67 68 @Override 69 public boolean onCreateOptionsMenu(Menu menu) { 70 // Inflate the menu; this adds items to the action bar if it is present. 71 getMenuInflater().inflate(R.menu.main, menu); 72 return true; 73 } 74 75 @Override 76 public boolean onOptionsItemSelected(MenuItem item) { 77 // Handle action bar item clicks here. The action bar will 78 // automatically handle clicks on the Home/Up button, so long 79 // as you specify a parent activity in AndroidManifest.xml. 80 int id = item.getItemId(); 81 if (id == R.id.action_settings) { 82 return true; 83 } 84 return super.onOptionsItemSelected(item); 85 } 86 }
时间: 2024-10-14 14:04:15