效果如下面的:

          

一共一个activity和两个xml。

******当我们须要使用的组件不在setContentView()设置的布局文件里,那我们就须要使用inflate()方法来获取。使用view对象调用findViewByid(),作者一开直接调用findViewByid。走了好多弯路,于是写此博文来帮助各位博友,知道了就非常easy了。

*******

MainActivity.java

package com.example.head;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory; public class MainActivity extends Activity implements ViewFactory{ ImageButton imageButton ;
Gallery gallery;
ImageSwitcher imageSwitcher; int index; private final static Integer[] IMAGES = {
R.drawable.z1,
R.drawable.z2,
R.drawable.z3 }; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); imageButton = (ImageButton)findViewById(R.id.IB01); imageButton.setBackgroundResource(R.drawable.z1); } public void click(View v){ LayoutInflater layoutInflater =LayoutInflater.from(MainActivity.this);
View view = layoutInflater.inflate(R.layout.headchoose, null);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); imageSwitcher= (ImageSwitcher)view.findViewById(R.id.IS01);
//组件不在activity_main。所以先要使用inflate获得headchoose布局
//并赋值给view对象。使用view对象,来调用findViewByid()方法。假设不用view来
//调用findViewByid(),找不到会报错。 gallery = (Gallery)view.findViewById(R.id.Gall01); imageSwitcher.setFactory(MainActivity.this); gallery.setAdapter(new ImageAdapter(MainActivity.this)); gallery.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
imageSwitcher.setImageResource(IMAGES[position]);
index = position; }
}); builder.setTitle("图片选择")
.setView(view)
.setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) {
imageButton.setBackgroundResource(IMAGES[index]); } });
builder.show(); } @Override
public View makeView() {
// TODO Auto-generated method stub
return new ImageView(this);
} /*
* ImageAdapter
*
*/ class ImageAdapter extends BaseAdapter{ Context context; public ImageAdapter(Context context){
this.context = context; }
@Override
public int getCount() {
// TODO Auto-generated method stub
return IMAGES.length;
} @Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
} @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
} @Override
public View getView(int position, View v, ViewGroup vg) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(IMAGES[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(120, 120));
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); return imageView;
}
} }

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/IB01"
android:layout_width="70dp"
android:layout_height="70dp"
android:onClick="click"/> <EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"/> </LinearLayout>

headchoose.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <Gallery
android:id="@+id/Gall01"
android:layout_width="fill_parent"
android:spacing="10dp"
android:layout_height="90dp" /> <ImageSwitcher
android:id="@+id/IS01"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_centerHorizontal="true">
</ImageSwitcher> </RelativeLayout>

版权声明:本文博主原创文章。博客,未经同意不得转载。

得知Android小遴选程序第七头(他们定义对话框、Gallery、ImageSwitcher)的更多相关文章

  1. Android 小闹钟程序

    最近写了个闹钟的程序,看到SharedPreferences在一个程序中可以共享数据,SharedPreferences是一个轻量级的键值存储机制,只可以存储基本数据类型.我就拿来用用,没想到Shar ...

  2. android小程序之幸运菜谱

    android小程序之幸运菜谱 前言:刚刚结束短短5天的android公开课程,收获不少,写下来记录一下吧!(因为学校校企公开课的缘故才偶然接触的android,所以只学了这几天,不喜勿喷) 一开始得 ...

  3. ubuntu下整合eclipse和javah生成jni头文件开发android的native程序

    0:前言: 这两天一直在研究用android的jni调用第三方库,上网搜方法,但是都是泛泛而谈,没有demo,经过我几番折磨,写了n多的helloword工程,总是不成功,工程名字也就由helloow ...

  4. android小程序-电子钢琴-多点触控

    我在第一篇博客<android小程序-电子钢琴-滑动连续响应>中实现了一个简单地7键钢琴,这几天把它又完善了一下,增加了多点触控,按键也增加了一个低音区和一个高音区,使得又可以多弹一点简单 ...

  5. Android群英传笔记——第七章:Android动画机制和使用技巧

    Android群英传笔记--第七章:Android动画机制和使用技巧 想来,最 近忙的不可开交,都把看书给冷落了,还有好几本没有看完呢,速度得加快了 今天看了第七章,Android动画效果一直是人家中 ...

  6. 《深入理解Android 卷III》第七章 深入理解SystemUI

    <深入理解Android 卷III>即将公布,作者是张大伟.此书填补了深入理解Android Framework卷中的一个主要空白,即Android Framework中和UI相关的部分. ...

  7. 一个简单的Android小实例

    原文:一个简单的Android小实例 一.配置环境 1.下载intellij idea15 2.安装Android SDK,通过Android SDK管理器安装或卸载Android平台   3.安装J ...

  8. 介绍一款Android小游戏--交互式人机对战五子棋

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6589025 学习Android系统开发之余,编 ...

  9. Android - 采用ApiDemos得知Android开展

    采用ApiDemos得知Android开展 本文地址: http://blog.csdn.net/caroline_wendy 位置: android-sdk/samples/android-20/l ...

随机推荐

  1. 利用Perf4j 对java项目进行性能监控

    Perf4j 可以对自定义监控范围的java代码进行日志记录,再经统计分析生成所需性能数据.Perf4j 提供了对常用日志工具log4j的扩展以方便与产品集成,它产生的性能数据可被用于生成可视化的性能 ...

  2. VLC笔记 它 立志

    不过,别忘了找工作的时候毕业,我说:"至少不会操心你会饿死了". 直到刚刚我才认为我妈有点过于乐观了. 今天下午,在做vlc如今播放器部分,一堆代码看的我头大. 正在此时,boss ...

  3. ASA failover应用

    failover的条件: 1.硬件型号必须相同 2.系统版本必须一致 3.模式必须一致 4.相同的许可和许可的数量 步骤: step 1:配置failover interface,确保状态 up LZ ...

  4. Java使用Socket传输文件遇到的问题

    1.写了一个socket传输文件的程序,发现传输过去文件有问题.找了一下午终于似乎找到了原因,记录下来警示一下: 接受文件的一端,向本地写文件之前使用Thread.sleep(time)休息一下就解决 ...

  5. ACdream 1114(莫比乌斯反演)

    传送门:Number theory 题意:给n个数,n 和 每个数的范围都是 1---222222,求n个数中互质的对数. 分析:处理出每个数倍数的个数cnt[i],然后进行莫比乌斯反演,只不过这里的 ...

  6. spring MVC拦截器01

    spring MVC拦截 作用:身份校验,权限检查,防止非法訪问. 场景:一个bbs系统,用户没有登录就无法发帖或者删除评论; 一个博客系统,没有登录就无法发表博文,无法添加分类,无法删除博文. sp ...

  7. Visual Studio Installer打包后生成的安装文件每次执行都需要重新安装C++ 2010运行库(x86)的解决方案

    原文:Visual Studio Installer打包后生成的安装文件每次执行都需要重新安装C++ 2010运行库(x86)的解决方案 如果你是用Visual Studio自带的打包项目打包了一个I ...

  8. C++ Primer笔记7_STL之关联容器

    关联容器 与顺序容器不同,关联容器的元素是按keyword来訪问和保存的.而顺序容器中的元素是按他们在容器中的位置来顺序保存的. 关联容器最常见的是map.set.multimap.multiset ...

  9. 自己定义 ViewGroup 支持无限循环翻页之三(响应回调事件)

    大家假设喜欢我的博客,请关注一下我的微博,请点击这里(http://weibo.com/kifile),谢谢 转载请标明出处,再次感谢 ################################ ...

  10. 第1次实验——NPC问题(回溯算法、聚类分析)

    题目:八皇后问题,是一个古老而著名的问题,是回溯算法的典型案例.该问题是国际西洋棋棋手马克斯·贝瑟尔于1848年提出:在8X8格的国际象棋上摆放八个皇后,使其不能互相攻击,即随意两个皇后都不能处于同一 ...