得知Android小遴选程序第七头(他们定义对话框、Gallery、ImageSwitcher)
效果如下面的:
一共一个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)的更多相关文章
- Android 小闹钟程序
最近写了个闹钟的程序,看到SharedPreferences在一个程序中可以共享数据,SharedPreferences是一个轻量级的键值存储机制,只可以存储基本数据类型.我就拿来用用,没想到Shar ...
- android小程序之幸运菜谱
android小程序之幸运菜谱 前言:刚刚结束短短5天的android公开课程,收获不少,写下来记录一下吧!(因为学校校企公开课的缘故才偶然接触的android,所以只学了这几天,不喜勿喷) 一开始得 ...
- ubuntu下整合eclipse和javah生成jni头文件开发android的native程序
0:前言: 这两天一直在研究用android的jni调用第三方库,上网搜方法,但是都是泛泛而谈,没有demo,经过我几番折磨,写了n多的helloword工程,总是不成功,工程名字也就由helloow ...
- android小程序-电子钢琴-多点触控
我在第一篇博客<android小程序-电子钢琴-滑动连续响应>中实现了一个简单地7键钢琴,这几天把它又完善了一下,增加了多点触控,按键也增加了一个低音区和一个高音区,使得又可以多弹一点简单 ...
- Android群英传笔记——第七章:Android动画机制和使用技巧
Android群英传笔记--第七章:Android动画机制和使用技巧 想来,最 近忙的不可开交,都把看书给冷落了,还有好几本没有看完呢,速度得加快了 今天看了第七章,Android动画效果一直是人家中 ...
- 《深入理解Android 卷III》第七章 深入理解SystemUI
<深入理解Android 卷III>即将公布,作者是张大伟.此书填补了深入理解Android Framework卷中的一个主要空白,即Android Framework中和UI相关的部分. ...
- 一个简单的Android小实例
原文:一个简单的Android小实例 一.配置环境 1.下载intellij idea15 2.安装Android SDK,通过Android SDK管理器安装或卸载Android平台 3.安装J ...
- 介绍一款Android小游戏--交互式人机对战五子棋
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6589025 学习Android系统开发之余,编 ...
- Android - 采用ApiDemos得知Android开展
采用ApiDemos得知Android开展 本文地址: http://blog.csdn.net/caroline_wendy 位置: android-sdk/samples/android-20/l ...
随机推荐
- Android开源项目大全 - 工具类
主要包括那些不错的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容.多媒体相关及其他. 一.依赖注入DI 通过依赖注入减少Vie ...
- 内存数据网格IMDG简单介绍
1 简单介绍 将内存作为首要存储介质不是什么新奇事儿,我们身边有非常多主存数据库(IMDB或MMDB)的样例.在对主存的使用上.内存数据网格(In Memory Data Grid,IMDG)与IMD ...
- 属性“dataProvider”有多个初始值设定项。(注意:“dataProvider”是“mx.charts.BarChart”的默认属性)。
1.错误描写叙述 属性"dataProvider"有多个初始值设定项.(注意:"dataProvider"是"mx.charts.BarChart&q ...
- nyist oj 311 全然背包 (动态规划经典题)
全然背包 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描写叙述 直接说题意,全然背包定义有N种物品和一个容量为V的背包.每种物品都有无限件可用.第i种物品的体积是c,价值是 ...
- oracle动态注冊參数local_listener
local_listener參数有两种书写格式,提供了不同的功能. 监听文件上,1521和1526port上都有动态监听port. [oracle@dbsv admin]$ cat listener. ...
- 使用RouteDebugger对MVC路由进行调试
在Asp.Net MVC程序中,路由是MVC程序的入口,每一个Http请求都要经过路由计算,然后匹配到相应的Controller和Action.通常我们的路由都会注册在Global.asax.cs文件 ...
- java多线程控制台聊天室(转)
用java多线程实现一个控制台聊天室,呵呵,好玩! 聊天室服务器端 package tf.thread; import java.io.BufferedReader; import java.io.I ...
- 【译】ASP.NET MVC 5 教程 - 1:入门
原文:[译]ASP.NET MVC 5 教程 - 1:入门 本教程将教你使用Visual Studio 2013 预览版构建 ASP.NET MVC 5 Web 应用程序 的基础知识.本主题还附带了一 ...
- cocos2d-x快乐的做让人快乐的游戏3:cocos-2d 3.x中的物理世界
Cocos2d-x 3.0+ 中全新的封装的物理引擎给了开发人员最大的便捷,你不用再繁琐与各种物理引擎的细节,全然的封装让开发人员能够更快更好的将物理引擎的机制加入�到自己的游戏中,简化的设计是从2. ...
- Knockout应用开发指南 第九章:高级应用举例
原文:Knockout应用开发指南 第九章:高级应用举例 1 Contacts editor 这个例子和微软为演示jQuery Data Linking Proposal例子提供的例子一样的提供的 ...