Android 基础UI编程
专业相框设计
ImageView 的堆叠应用
① 新建工程
② 准备三张png 图片
left.png right.png photo.png
③ 修改main.xml 布局,添加UI 元素
 <?xml version="1.0" encoding="utf-8"?><AbsoluteLayoutandroid:id="@+id/widget34"android:layout_width="fill_parent"android:layout_height="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android"><!--创建第一个ImageView (第二层图片)--><ImageViewandroid:id="@+id/myImageView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_x="0px"android:layout_y="36px"/><!--创建第二个ImageView (第一层图片)--><ImageViewandroid:id="@+id/myImageView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_x="0px"android:layout_y="36px"/><!--创建第一个Button --><Buttonandroid:id="@+id/myButton1"android:layout_width="105px"android:layout_height="66px"android:text="pic1"android:layout_x="9px"android:layout_y="356px"/><!--创建第二个Button --><Buttonandroid:id="@+id/myButton2"android:layout_width="105px"android:layout_height="66px"android:text="pic2"android:layout_x="179px"android:layout_y="356px"/></AbsoluteLayout>

④ 修改mainActivity.java
package zyf.Ex_Ctrl_7;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.ImageView;public class Ex_Ctrl_7 extends Activity {/** Called when the activity is first created. *//* 声明Button、ImageView 对象*/private ImageView mImageView01;private ImageView mImageView02;private Button mButton01;private Button mButton02;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);/* 取得Button、ImageView 对象*/mImageView01 = (ImageView) findViewById(R.id.myImageView1);mImageView02 = (ImageView) findViewById(R.id.myImageView2);mButton01 = (Button) findViewById(R.id.myButton1);mButton02 = (Button) findViewById(R.id.myButton2);/* 设置ImageView 背景图*/mImageView01.setImageDrawable(getResources().getDrawable(R.drawable.right));mImageView02.setImageDrawable(getResources().getDrawable(R.drawable.photo));/* 用OnClickListener 事件来启动*/mButton01.setOnClickListener(new Button.OnClickListener() {@Overridepublic void onClick(View v) {/* 当启动后, ImageView 立刻换背景图*/mImageView01.setImageDrawable(getResources().getDrawable(R.drawable.right));}});mButton02.setOnClickListener(new Button.OnClickListener() {@Overridepublic void onClick(View v) {mImageView01.setImageDrawable(getResources().getDrawable(R.drawable.left));}});}}

ImageButton 的堆叠应用
① 新建项目
② 准备三张png 图片
left.png right.png photo.png
③ 修改main.xml 布局,添加UI 元素
<?xml version="1.0" encoding="utf-8"?><AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><ImageButtonandroid:id="@+id/myImageButton_Back"android:state_focused="true"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_x="0px"android:layout_y="36px"/><ImageButtonandroid:id="@+id/myImageButton_Photo"android:state_focused="true"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_x="0px"android:layout_y="36px"/></AbsoluteLayout>

④ 修改mainActivity.java
设置成堆叠
package zyf.Ex_Ctrl_7_B;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.ImageButton;public class Ex_Ctrl_7_B extends Activity {/** Called when the activity is first created. *//*声明ImageButton*/private ImageButton back_Imagebutton,photo_Imagebutton;private boolean Tag=true;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);/*从XML中获取控件对象*/back_Imagebutton=(ImageButton)findViewById(R.id.myImageButton_Back );photo_Imagebutton=(ImageButton)findViewById(R.id.myImageButton_Photo );//设置默认的背景图片back_Imagebutton.setBackgroundResource(R.drawable.left);photo_Imagebutton.setBackgroundResource(R.drawable.photo);//给ImageButton设置事件监听器photo_Imagebutton.setOnClickListener(new ImageButton.OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubTag=!Tag;//更改背景图片if(Tag){back_Imagebutton.setBackgroundResource(R.drawable.right);}else{back_Imagebutton.setBackgroundResource(R.drawable.left);}}});}}

更多相关文章

  1. Android之LinearLayout(线性布局)
  2. Android 布局加载源代码分析
  3. Android 部分内容设置颜色、字体、超链接、图片
  4. Android UI布局
  5. (Android)处理图片成圆形
  6. MixtureTextView 支持Android图文混排、文字环绕图片等效果

随机推荐

  1. Android studio gradle 多渠道打包
  2. Android之——常见Bug及其解决方案
  3. Android 编译环境快速安装(基于ubuntu 12
  4. android通过servlet服务器保存文件到手机
  5. Afaria 做Android设备lock时候发给GCM的
  6. JavaGG android实现uc 和墨迹天气那样的
  7. Android: not single entry point
  8. android的AndroidManifest.xml文件介绍
  9. Android 代码获取手机ip地址(个人笔记)
  10. Android 开发中,pullToRefreshListView