<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. Java并发编程:线程的基本状态

    一.线程的基本状态 线程基本上有5种状态,分别是:NEW.Runnable.Running.Blocked.Dead. 1)新建状态(New) 当线程对象对创建后,即进入了新建状态,如:Thread ...

  2. Spring Dubbo 开发笔记(一)——概述

    概述: Spring Dubbo 是我自己写的一个基于spring-boot和dubbo,目的是使用Spring boot的风格来使用dubbo.(即可以了解Spring boot的启动过程又可以学习 ...

  3. Kafka0.10.2.0分布式集群安装

    一.依赖文件安装 1.1 JDK 参见博文:http://www.cnblogs.com/liugh/p/6623530.html 1.2 Scala 参见博文:http://www.cnblogs. ...

  4. Opencv4android的Android Studio项目配置及实例下载

    因为软件竞赛的项目会用到Opencv fo rAndroid,所以就研究了一下如何在Android Studio上配置Opencv4Android 环境概述: Android Studio 2.3 O ...

  5. Vxlan与网卡offload性能

    背景 由于数据链路层MTU的限制,发送端TCP/UDP数据在交付到IP层时需要与MTU相匹配,TCP数据不能超过mss,较长的UDP需要分片(Fragmentation)以满足MTU要求:接收端协议栈 ...

  6. maven的web项目手工发布

    网上有很多人都认为maven的web项目发布时,要用这个命令那个命令,太麻烦了,用纯手工打造发布更为方便. 第一步,导出你所在项目maven的依赖包. 打开cmd窗口执行命令:mvn dependen ...

  7. QT5 && VS2013配置

    1.下载安装包 qt-opensource-windows-x86-msvc2013_64-5.7.0.exe VS2013_RTM_ULT_ENU.iso 插件:qt-vs-addin-msvc20 ...

  8. 简单的RTSP消息交互过程

    C表示RTSP客户端,S表示RTSP服务端 第一步:查询服务器端可用方法 1. C->S:OPTION request       //询问S有哪些方法可用 2. S->C:OPTION ...

  9. css 画出三角形

    技术分享不一定行文累赘 这里说说最简洁的 css 画出三角形 display: inline-block; border: 10px dashed transparent; border-left: ...

  10. angular 实现时间段选择组件

    前段时间公司有个后台项目需要使用一个选择时间段的组件,看了一下就自己写了一下,用angular写这种插件还是很简单的. 先看看最终是什么样子的: 功能是用户可以选择任意时间段,鼠标可以拖动任意的一周的 ...