您的足迹:首页 > 移动开发 >Android实现点击更换图片

Android实现点击更换图片

Android的xml文件activity_frame_layout_aty.xm,也就是界面

<FrameLayout 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.myfirstapp.FrameLayoutAty"
    android:id="@+id/root" >

     <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/a"  />
<ImageView
        android:id="@+id/imageView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/b" 
        android:visibility="invisible"/>

</FrameLayout>

 

java代码FrameLayoutAty.java


package com.example.myfirstapp;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class FrameLayoutAty extends ActionBarActivity {
	  private FrameLayout root;
	  private ImageView a, b;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_frame_layout_aty);
		root=(FrameLayout) findViewById(R.id.root);
		a=(ImageView) findViewById(R.id.imageView1);
		b=(ImageView) findViewById(R.id.imageView2);
		showA();
		root.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(a.getVisibility()==View.VISIBLE){
					showB();
				}
				else{
					showA();
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.frame_layout_aty, 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);
	}
	public void showA(){
		a.setVisibility(View.VISIBLE);
		b.setVisibility(View.INVISIBLE);
	}
	public void showB(){
		b.setVisibility(View.VISIBLE);
		a.setVisibility(View.INVISIBLE);
	}
}
 


配置文件AndroidManifest.xml添加下面代码

<activity
            android:name=".FrameLayoutAty"
            android:label="@string/title_activity_frame_layout_aty" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

 QQ图片20150623214741.png

QQ图片20150623214746.png

 

本博客所有文章如无特别注明均为原创。作者:小天复制或转载请以超链接形式注明转自 钟天才的博客
原文地址《Android实现点击更换图片

相关推荐

网友评论(0)