效果如下面的:

          

一共一个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. Tiny server:小型Web服务器

    一.背景 csapp的网络编程粗略的介绍了关于网络编程的一些知识,在最后的一节主要就实现了一个小型的Webserver.这个server名叫Tiny,它是一个小型的可是功能齐全的Webserver.在 ...

  2. Delphi过程函数传递参数的八种方式

    今天一同事问我为什么有些过程函数里面有Var而有些没有,不解,遂到网上百度,得解.快哉,快哉. 在Delphi过程.函数中传递参数几个修饰符为Const.Var.Out.另一种不加修饰符的为默认按值传 ...

  3. 关于JAVA Project.waitfor()返回值是1

    Project.waitfor()返回值是1,找了很久从网上没有发现关于1的说明. 这时对源代码调试了一下,发现Project=null.而去根目录下点击被调用的bat文件发现也可以被正确执行. 这时 ...

  4. poj3613(恰经过N条边的最短路)

    题目连接:http://poj.org/problem?id=3613 题意:从S 到 T 经过边得个数恰为k的最短路是多少. 分析:01邻接矩阵A的K次方C=A^K,C[i][j]表示i点到j点正好 ...

  5. nginx学习12 ngx_cycle_t 和 ngx_init_cycle

    在nginx在启动过程,ngx_init_cycle这个函数最初始工作.变量的初始化存储在ngx_cycle_t这个结构体中,为了深入了解这个函数都做了那些初始化工作,就化时间研究了一下.并写下来以便 ...

  6. Matlab spline

    请记住,,平稳 早期project图时,把富有弹性的细长木条(所谓样条)用压铁固定在样点上,在其它地方让它自由弯曲,然后沿木条画下曲线. 成为样条曲线 三次样条插值(简称Spline插值)是通过一系列 ...

  7. 《Cocos2d-x实战(卷Ⅰ):C++开发》

    <Cocos2d-x实战(卷Ⅰ):C++开发>   基础篇 第1章    准备开始 1.1   本书学习路线图 1.2   使用实例代码   第2章    Cocos2d-x介绍与环境搭建 ...

  8. 在VS2012中使用GitHub

    注册GitHub账号(DeanZhouLin) https://github.com/ 向GitHub中添加一个仓库(Test) *创建完成后,记录该仓库的地址:https://github.com/ ...

  9. Android横屏竖屏设置

    Android横竖屏设置: 方法一:onCreate()中 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); // ...

  10. 图像库---Image Datasets---OpenSift源代码---openSurf源代码

    1.Computer Vision Datasets on the web http://www.cvpapers.com/datasets.html 2.Dataset Reference http ...