需求:
点击置左和置右按钮,底层图像进行切换,上层图像保持不动
代码如下:
private ImageView myImageView1;
private ImageView myImageView2;
private Button myButtonLeft;
private Button myButtonRight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myImageView1 = (ImageView)findViewById(R.id.myImageView1);
myImageView2 = (ImageView)findViewById(R.id.myImageView2);
myButtonLeft = (Button)findViewById(R.id.button1);
myButtonRight = (Button)findViewById(R.id.button2);
myImageView1.setImageDrawable(getResources().getDrawable(R.drawable.right));
myImageView1.setImageDrawable(getResources().getDrawable(R.drawable.oa));
myButtonLeft.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
myImageView1.setImageDrawable(getResources().getDrawable(R.drawable.left));
}});
myButtonRight.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
myImageView1.setImageDrawable(getResources().getDrawable(R.drawable.right));
}});
XML:
/*底层图像*/
<ImageView
android:id="@+id/myImageView1"
android:layout_width="320px"
android:layout_height="280px"
android:layout_x="0px"
android:layout_y="36px" />
/*上层图像*/
<ImageView
android:id="@+id/myImageView2"
android:layout_width="104px"
android:layout_height="157px"
android:layout_x="101px"
android:layout_y="119px"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="66dp"
android:text="Left" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button1"
android:layout_alignParentRight="true"
android:layout_marginRight="40dp"
android:text="Right" />
ImageView的放置顺序和图层的顺序有关
重点:
ImageView设置图片
myImageView1.setImageDrawable(getResources().getDrawable(R.drawable.right));
eclipse 项目文件浏览界面消失了:
解决方式:WINDOW--->SHOW VIEW-->Package Exploer
在res/drawable中添加图片的方式:
将图片粘贴到项目res/drawable的文件夹下,重启一下eclipse就可以了(暂时是我采用的,应该有不用重启的办法)