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文件,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.imagebutton.MainActivity" > <LinearLayout
android:id="@+id/layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="18dp"
android:src="@drawable/a" /> <RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/spinner1"
android:layout_below="@+id/spinner1"
android:layout_marginTop="29dp" >
</RadioGroup> </LinearLayout> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/layout1"
android:layout_below="@+id/layout1"
android:text="上一个" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button1"
android:layout_centerHorizontal="true"
android:text="下一个" /> </RelativeLayout>
接下来实现点击上一个或者下一个按钮,图片会发生改变功能,代码如下:
package com.example.imagebutton; import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView; public class MainActivity extends Activity { private Button prebutton = null;
private Button nextbutton = null;
private ImageView imageview1 = null;
public int i = 0;
private int[ ] iImages = {R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e,R.drawable.f}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); prebutton = (Button)findViewById(R.id.button1);
nextbutton = (Button)findViewById(R.id.button2);
imageview1 = (ImageView)findViewById(R.id.imageView1);
prebutton.setOnClickListener(new prebuttonclick());
nextbutton.setOnClickListener(new nextbuttonclick()); imageview1.setImageResource(i);
} public class prebuttonclick implements OnClickListener { @Override
public void onClick(View v) {
if (i > 0)
{
imageview1.setImageResource(iImages[--i]);
}
else if (i <= 0)
{
i = 5;
imageview1.setImageResource(iImages[i]);
}
}
} public class nextbuttonclick implements OnClickListener {
@Override
public void onClick(View v) {
if (i < 5)
{
imageview1.setImageResource(iImages[++i]);
}
else if (i >= 5)
{
i = 0;
imageview1.setImageResource(iImages[i]);
} }
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}