<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布局的更多相关文章

  1. Android Gallery和ImageSwitcher同步自动(滚动)播放图片库

    本文主要内容是如何让Gallery和ImageSwitcher控件能够同步自动播放图片集 ,看起来较难,然而,实现的方法非常简单, 请跟我慢慢来.总的来说,本文要实现的效果如下图:(截图效果不怎么好) ...

  2. Android中Gallery和ImageSwitcher同步自动(滚动)播放图片库

    本文主要内容是如何让Gallery和ImageSwitcher控件能够同步自动播放图片集 ,看起来较难,然而,实现的方法非常简单, 请跟我慢慢来.总的来说,本文要实现的效果如下图:(截图效果不怎么好) ...

  3. 得知Android小遴选程序第七头(他们定义对话框、Gallery、ImageSwitcher)

    效果如下面的:            一共一个activity和两个xml. ******当我们须要使用的组件不在setContentView()设置的布局文件里,那我们就须要使用inflate()方 ...

  4. 安卓开发笔记——Gallery组件+ImageSwitcher组件

    什么是Gallery? Gallery是一个水平的列表选择框,它允许用户通过拖动来查看上一个.下一个列表选项. 下图是今天要实现的最终效果: 利用Gallery组件实现的一个横向显示图像列表,可以通过 ...

  5. 安卓Gallery配合ImageSwitcher不显示图片

    Gallary装的是缩略图(thumb),ImageSwitcher装的是大图. 不显示图片的一个可能原因是gallery没设置代理器,另一个原因是没使用相对布局. GalleryActivity.j ...

  6. 【问题】Expandable数据集的定义的正确方法,TabActivity弃用替代,Gallery替代,imageswitcher

    Expandable 问题: http://www.cnblogs.com/xingyyy/p/3389611.html 扩展阅读:http://blog.csdn.net/lmj623565791/ ...

  7. Android学习Tabhost、gallery、listview、imageswitcher

    Tabhost控件又称分页控件,在很多的开发语言中都存在.它可以拥有多个标签页,每个标签页可以拥有不同的内容.android中,一个标签页可以放 一个view或者一个activity.TabHost是 ...

  8. Android 高级UI设计笔记13:Gallery(画廊控件)之 循环显示图像

    1. 循环显示图像的原理  循环显示有些类似于循环链表,最后一个结点的下一个结点又是第1个结点.循环显示图像也可以模拟这一点. 也许细心的读者从上一节实现的ImageAdapter类中会发现些什么.对 ...

  9. Android开发学习之Gallery和GridView浅析

    一.Gallery的简介 Gallery(画廊)是一个锁定中心条目并且拥有水平滚动列表的视图,一般用来浏览图片,并且可以响应事件显示信息.Gallery还可以和ImageSwitcher组件结合使用来 ...

随机推荐

  1. 老李分享:android手机测试之适配(1)

    Android的屏幕适配一直以来都在折磨着我们这些开发者,本篇文章以Google的官方文档为基础,全面而深入的讲解了Android屏幕适配的原因.重要概念.解决方案及最佳实践,我相信如果你能认真的学习 ...

  2. TextField和TextView

    本文概要 1.简介 2.介绍TextField控件 3.介绍TextView控件 4.键盘的打开和关闭 5.关闭和大开键盘的通知 6.键盘的种类 详情 1.简介 与Label一样,TextField和 ...

  3. 跟着刚哥梳理java知识点——枚举和注解(十四)

    enum Season{ SPRING("spring","春暖花开"), SUMMER("summer","夏日炎炎" ...

  4. .exe简单的更新软件demo

    百度网盘源码加文件:http://pan.baidu.com/s/1qYe2Vgg 功能:通过网站更新用户的软件,需要联网,也可以通过本地网站更新局域网用户软件. 根本实现:1.一个网站(本地就可以) ...

  5. ORACLE中关于外键缺少索引的探讨和总结

    在ORACLE数据库中,定义外键约束时,ORACLE是不会自动创建对应索引的,必须手动在外键约束相关的列上创建索引.那么外键字段上是否有必要创建索引呢?如果有必要的话,巡检时,如何找出外键字段上没有创 ...

  6. Spring+SpringMVC+MyBatis+easyUI整合优化篇(十)数据层优化-整合druid

    druid介绍 这是druid对自己的介绍: Druid是阿里开源的一个数据库连接池技术,号称自己是目前最好的数据库连接池,在功能.性能.扩展性方面,都超过其他数据库连接池,包括DBCP.C3P0.B ...

  7. angular的$http.post()提交数据到Java后台接收不到参数值问题的解决方法

    本文地址:http://www.cnblogs.com/jying/p/6733408.html   转载请注明出处: 写此文的背景:在工作学习使用angular的$http.post()提交数据时, ...

  8. this web application instance has been stopped already.

    this web application instance has been stopped already. Could not load oracle/sql/converter_xcharset ...

  9. stm32中的延时函数

    //粗延时函数,微秒 void delay_nus(u16 time) { u16 i=0; while(time--) { i=10;  //自己定义 while(i--) ; } } //毫秒级的 ...

  10. python——迭代器和生成器

    1.迭代器 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退.另外,迭代器的一大优 ...