探索Gallery和ImageSwitcher布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Gallery
android:id="@+id/img_gallery"
android:layout_width="fill_parent"
android:layout_height="110px"
android:layout_marginTop = "100px"
android:layout_alignParentLeft="true"
/>
<ImageSwitcher
android:id="@+id/image_switcher"
android:layout_width="90px"
android:layout_height="90px"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_alignBottom="@+id/img_gallery" ></ImageSwitcher>
</RelativeLayout>

当把上面Gallery一项属性改成android:layout_marginTop = "0px",出现下面的效果

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Gallery
android:id="@+id/img_gallery"
android:layout_width="fill_parent"
android:layout_height="110px"
android:layout_marginTop = "0px"
android:layout_alignParentLeft="true"
/>
<ImageSwitcher
android:id="@+id/image_switcher"
android:layout_width="90px"
android:layout_height="90px" android:layout_centerHorizontal="true"
android:layout_alignBottom="@+id/img_gallery" ></ImageSwitcher>
</RelativeLayout>
删掉其中一行
PS: iv.setLayoutParams(new Gallery.LayoutParams(80, 80)); iv.setPadding(15, 10, 15, 10);//setPadding(int left, int top, int right, int bottom)
iv.setLayoutParams(new ImageSwitcher.LayoutParams(90,90));
Gallery中的图像居中显示,单个的图像的实际大小为60*60,虽然代码中指定为80*80,但还需要减去上下Padding,背景画布的高度为110,在xml中指定。
若想整体放大,画布高度调整为300,选中图片大小为160*160,队列图片大小为130*130。且选中图片刚好居中覆盖后面的图片。
75
10
130 160 则Switcher距离最下面的高度应该为 75+10- (160-130)/2 = 70,这个70应该设置为Switcher的layout_marginBottom
10
75
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Gallery
android:id="@+id/img_gallery"
android:layout_width="fill_parent"
android:layout_height="300px"
android:layout_marginTop = "0px"
android:layout_alignParentLeft="true"
/>
<ImageSwitcher
android:id="@+id/image_switcher"
android:layout_width="160px"
android:layout_height="160px"
android:layout_marginBottom = "70px"
android:layout_centerHorizontal="true"
android:layout_alignBottom="@+id/img_gallery" ></ImageSwitcher>
</RelativeLayout>
达到预期效果:

完整控制代码:
package com.hixin.contact;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory; public class MainActivity extends Activity { ImageButton btn_img;
AlertDialog imageChooseDialog;
Gallery gallery;
ImageSwitcher is; private int[] images ={ R.drawable.image1,R.drawable.image2,R.drawable.image3,
R.drawable.image4,R.drawable.image5,R.drawable.image6,
R.drawable.image7,R.drawable.image8,R.drawable.image9,
R.drawable.image10,R.drawable.image11,R.drawable.image12,
R.drawable.image13,R.drawable.image14,R.drawable.image15,
R.drawable.image16,R.drawable.image17,R.drawable.image18,
R.drawable.image19,R.drawable.image20,R.drawable.image21,
R.drawable.image22,R.drawable.image23,R.drawable.image24,
R.drawable.image25,R.drawable.image26,R.drawable.image27,
R.drawable.image28,R.drawable.image29,R.drawable.image30}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.addnew);
btn_img = (ImageButton) this.findViewById(R.id.btn_img);
btn_img.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
initImageChooseDialog();
imageChooseDialog.show();
} });
}
protected void initImageChooseDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择头像");
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.imageswitch,null);
gallery = (Gallery) view.findViewById(R.id.img_gallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setSelection(images.length/2);
gallery.setOnItemSelectedListener(new OnItemSelectedListener() { @Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) { is.setImageResource(images[position]);
} @Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub } });
is = (ImageSwitcher) view.findViewById(R.id.image_switcher);
is.setFactory(new MyViewFactory(this));
builder.setView(view);
imageChooseDialog = builder.create(); } class ImageAdapter extends BaseAdapter {
private 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 null;
} @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView iv = new ImageView(context);
iv.setImageResource(images[position]);
iv.setAdjustViewBounds(true);
iv.setLayoutParams(new Gallery.LayoutParams(150,150));
iv.setPadding(15, 10, 15, 10);
return iv;
} }
class MyViewFactory implements ViewFactory {
private Context context;
public MyViewFactory(Context context) {
this.context = context;
}
@Override
public View makeView() {
ImageView iv = new ImageView(context);
iv.setAdjustViewBounds(true);
iv.setLayoutParams(new ImageSwitcher.LayoutParams(160,160));
return iv;
} } }
探索Gallery和ImageSwitcher布局的更多相关文章
- Android Gallery和ImageSwitcher同步自动(滚动)播放图片库
本文主要内容是如何让Gallery和ImageSwitcher控件能够同步自动播放图片集 ,看起来较难,然而,实现的方法非常简单, 请跟我慢慢来.总的来说,本文要实现的效果如下图:(截图效果不怎么好) ...
- Android中Gallery和ImageSwitcher同步自动(滚动)播放图片库
本文主要内容是如何让Gallery和ImageSwitcher控件能够同步自动播放图片集 ,看起来较难,然而,实现的方法非常简单, 请跟我慢慢来.总的来说,本文要实现的效果如下图:(截图效果不怎么好) ...
- 得知Android小遴选程序第七头(他们定义对话框、Gallery、ImageSwitcher)
效果如下面的: 一共一个activity和两个xml. ******当我们须要使用的组件不在setContentView()设置的布局文件里,那我们就须要使用inflate()方 ...
- 安卓开发笔记——Gallery组件+ImageSwitcher组件
什么是Gallery? Gallery是一个水平的列表选择框,它允许用户通过拖动来查看上一个.下一个列表选项. 下图是今天要实现的最终效果: 利用Gallery组件实现的一个横向显示图像列表,可以通过 ...
- 安卓Gallery配合ImageSwitcher不显示图片
Gallary装的是缩略图(thumb),ImageSwitcher装的是大图. 不显示图片的一个可能原因是gallery没设置代理器,另一个原因是没使用相对布局. GalleryActivity.j ...
- 【问题】Expandable数据集的定义的正确方法,TabActivity弃用替代,Gallery替代,imageswitcher
Expandable 问题: http://www.cnblogs.com/xingyyy/p/3389611.html 扩展阅读:http://blog.csdn.net/lmj623565791/ ...
- Android学习Tabhost、gallery、listview、imageswitcher
Tabhost控件又称分页控件,在很多的开发语言中都存在.它可以拥有多个标签页,每个标签页可以拥有不同的内容.android中,一个标签页可以放 一个view或者一个activity.TabHost是 ...
- Android 高级UI设计笔记13:Gallery(画廊控件)之 循环显示图像
1. 循环显示图像的原理 循环显示有些类似于循环链表,最后一个结点的下一个结点又是第1个结点.循环显示图像也可以模拟这一点. 也许细心的读者从上一节实现的ImageAdapter类中会发现些什么.对 ...
- Android开发学习之Gallery和GridView浅析
一.Gallery的简介 Gallery(画廊)是一个锁定中心条目并且拥有水平滚动列表的视图,一般用来浏览图片,并且可以响应事件显示信息.Gallery还可以和ImageSwitcher组件结合使用来 ...
随机推荐
- 关于Form表单一些基础知识
1.两个重要属性: action:表单需要提交的服务器地址 method:表单提交数据使用的方法,get/post >>>get和post的区别 ①get传参使用URL传递,所有参数 ...
- 前端开发框架简介:angular和react
作者:vienwu react是facebook推出一个用来构建用户界面的js库.官方介绍的三大特性如下: just the ui 把react只当作一个ui组件就好,等同于传统mvc中的view. ...
- Windows下JIRA6.3.6安装、汉化、破解
一.MySQL建库和建账号 1. mysql中创建数据库jiradb create database jiradb character set 'UTF8'; 2.创建数据库用户并赋于权限 creat ...
- 初学Java scirpt(判断、循环语句)
在编写代码时,我们经常需要为不同的判断结果来执行不同的动作以及需要反复执行同一段代码,这时我们就需要使用判断和循环语句来实现. 1.判断语句(if) 判断语句经常用的有(if......else).( ...
- 利用 force recovery 解决服务器 crash 导致 MySQL 重启失败的问题
小明同学在本机上安装了 MySQL 5.7.17 配合项目进行开发,并且已经有了一部分重要数据.某天小明在开发的时候,需要出去一趟就直接把电脑关掉了,没有让 MySQL 正常关闭,重启 MySQL 的 ...
- 【shell编程基础1】shell变量篇
Bash shell bash shell 是bourne shell 的升级版,“bourne again shell”.ubuntu的默认shell. 预备知识 1. "#!" ...
- Mysql安装设置建议(参数设置)
当我们监测MySQL性能时,人们希望我们能够检视一下MySQL配置然后给出一些提高建议.许多人在事后都非常惊讶,因为我们建议他们仅仅改动几个设置,即使是这里有好几百个配置项.这篇文章的目的在于给你一份 ...
- 【转载】 stm32之PWM
发现这位博主的博客被大量的转发,我也转载一篇,谁叫人家写的好呢. 原文地址:http://blog.sina.com.cn/s/blog_49cb42490100s6uh.html 脉冲宽度调制(PW ...
- Ubuntu搭建mysql,Navicat Premium连接
保存编辑结果与退出vim编辑器 https://jingyan.baidu.com/article/495ba8410ff14d38b30ede01.html 首先,我们需要使用apt安装mysql, ...
- 谈一下我们是如何开展code review的
众所周知,代码审查是软件开发过程中十分重要的环节,楼主结合自己的实际工作经验,和大家分享一下在实际工作中代码审查是如何开展的, 笔者水平有限,若有错误和纰漏,还请大家指正. 代码审查的阻力 我想不通公 ...