效果如下面的:

          

一共一个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. Visual Assist X 10.8.2052的Crack破解补丁. 2014.11.05 (General release.)

    VA小组立即更新频率似有些放缓几乎,版本号都是2个月才更新一次,而这次的更新幅度也还是比較大的,新增了6个特性,而且修正和20余处大大小小的问题,而且也是正式发行版,推荐更新. 对于破解补丁还是老规矩 ...

  2. poj3678(two-sat)

    传送门:Katu Puzzl 题意:n个点,点的取值可以是0或者1.m条边,有权值,有运算方式(并,或,异或),要求和这条边相连的两个点经过边上的运算后的结果是边的权值.问你有没有可能把每个点赋值满足 ...

  3. xcode 改动整个项目名

    目标为:将项目名XCD4改成xcd5. 先上结果图:==> 1.在左側的导航区域点击两次项目名,两次间隔时间略微长些,项目名会变成可编辑状态. 将名称改动为xcd5后按enter键弹出一个对话框 ...

  4. C语言数组

    在C语言中,对于三维或三维以上数组的使用并没有很好的支持,而且使用率也非常的低,后面会对三维数组做一些简单的分析,这篇文章主要以二维数组来探讨一些C语言中数组使用的相关概念和技巧. 1 一个var[i ...

  5. hdu1876(dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1876 题意:问机器人到达终点的过程中最多有几次完全消耗完能量,消耗完这么多次能量的方式有几种. 分析: ...

  6. gem5 设定checkpiont以及从checkpoint开始运行

    同spec2006中间bzip2一个例子,如何设置checkpoint .以及从checkpoint继续以启动运行.这样做的目的是为了,采纳automic运行N指令,然后detailed运行M指令. ...

  7. mongodb常用操作语句

    mongodb常用操作语句 A:创建数据表 db.createCollection(name, {capped: <Boolean>, autoIndexId: <Boolean&g ...

  8. WPF界面设计技巧(11)-认知流文档 & 小议WPF的野心

    原文:WPF界面设计技巧(11)-认知流文档 & 小议WPF的野心 流文档是WPF中的一种独特的文档承载格式,它的书写和呈现方式都很像HTML,它也几乎具备了HTML的绝大多数优势,并提供了更 ...

  9. java枚举的使用

    定义枚举 public enum YesOrNo { YES("是") , NO("否") ; private String text ; private in ...

  10. JavaScript类数组对象参考

    JavaScript和DOM中有很多类数组对象,它们有以下特点 1.有length属性 2.可以使用[]通过下标访问 3.部分类数组对象使用[]访问成员时不只可以使用下标,还可以使用id或name 4 ...