效果如下面的:

          

一共一个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. 无需安装Mono的Jexus

    ASP.NET跨平台实践:无需安装Mono的Jexus“独立版”   在Linux上运行ASP.NET网站或WebApi的传统步骤是,先安装libgdiplus,再安装mono,然后安装Jexus.在 ...

  2. 鼠标右键怎么清除Catalyst Control Center

    开始→运行→regedit→找到HKEY_CLASSES_ROOT\Directory\Background\shellex\ContextMenuHandlers\ACE→双击并修改其键值 可以删除 ...

  3. 基于SIFT+Kmeans+LDA的图片分类器的实现

    原地址:http://www.cnblogs.com/freedomshe/archive/2012/04/24/2468747.html 题记:2012年4月1日回到家,南大计算机研究僧复试以后,等 ...

  4. MySQL JDBC事务处理、封装JDBC工具类

    MySQL数据库学习笔记(十)----JDBC事务处理.封装JDBC工具类 一.JDBC事务处理: 我们已经知道,事务的概念即:所有的操作要么同时成功,要么同时失败.在MySQL中提供了Commit. ...

  5. haskell,lisp,erlang你们更喜欢哪个?

    haskell,lisp,erlang你们更喜欢哪个? haskell,lisp,erlang你们更喜欢哪个?

  6. poj 1991 Turning in Homework dp

    这个可以证明必须从两边的任务开始交起,因为中间交的任务可以后面经过的时候再交,所以就变成了一个n*n的dp. #include <iostream> #include <cstdio ...

  7. 在MVC应用程序中动态加载PartialView

    原文:在MVC应用程序中动态加载PartialView 有时候,我们不太想把PartialView直接Render在Html上,而是使用jQuery来动态加载,或是某一个事件来加载.为了演示与做好这个 ...

  8. 解决com.ibatis.sqlmap.client.SqlMapException: There is no statement named in this SqlMap

    com.ibatis.sqlmap.client.SqlMapException: There is no statement named in this SqlMap. 可能存在3种情况: 1.在x ...

  9. MyEclipse配置启动多个Tomcat

    在实际开发中常常会遇到多个项目同一时候进行,来回切换不太方便,这时可分别部署在多个tomcat下. 改动一些配置可同一时候启动多个tomcat 一開始查阅相关文档,看到一篇文章一个Myeclipse同 ...

  10. unix解释器文件详解

    exec执行普通文件和解释器文件的区别 2014-11-15 23:52:45 分类: LINUX exec执行普通文件和解释器文件的区别 ——lvyilong316 1. 从一个问题开始 首先要从项 ...