您的位置:首页 > 移动开发 > Android开发

android应用开发详解(九)-----------------各种视图(续)

2014-09-28 09:09 411 查看
在家里,父母对我们百依百顺;
在外面,我们对社会百依百顺。
三、画廊视图Gallery

Gallery能够水平方向显示其内容,一般用来浏览图片,被选中的选项位于中间,并且可以响应事件显示信息。

1、主文件

package com.example.test_gallery;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
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.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

public class Test_Gallery_Activity extends Activity implements
OnItemSelectedListener, ViewFactory {
private ImageSwitcher mSwitcher;
// 需要准备大和小相对应的图片
private Integer[] mThumIds = { R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher };
private Integer[] mImageIds = { R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher };
private Context mContext;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置窗口无标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.test_gallery);
mSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcher01);
// 设置工厂
mSwitcher.setFactory(this);
// 设置动画效果
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
Gallery g = (Gallery) findViewById(R.id.gallery01);
// 添加adapter和onItemSelectedListener监听器
g.setAdapter(new ImageAdapter(this));
g.setOnItemSelectedListener(this);
}

@Override
public void onItemSelected(AdapterView<?> adapter, View v, int position,
long id) {
// 设置图片资源
mSwitcher.setImageResource(mImageIds[position]);

}

@Override
public void onNothingSelected(AdapterView<?> args) {

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.test__gallery_, menu);
return true;
}

@Override
public View makeView() {
// TODO Auto-generated method stub
ImageView i = new ImageView(this);
// 设置背景颜色
i.setBackgroundColor(0xFF000000);
// 设置精度类型
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
// 设置布局参数
i.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

return i;
}

// 内部类

public class ImageAdapter extends BaseAdapter {

public ImageAdapter(Context c) {
mContext = c;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return mThumIds.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 convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView i = new ImageView(mContext);
i.setImageResource(mThumIds[position]);
i.setAdjustViewBounds(true);
i.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// 背景图片需要重新设置,效果更明显一些。
i.setBackgroundResource(R.drawable.ic_launcher);
return i;
}

}

}


2、布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ImageSwitcher
android:id="@+id/imageswitcher01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ImageSwitcher>

<Gallery
android:id="@+id/gallery01"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#55000000"
android:spacing="16dp"
android:gravity="center_vertical"/>

</RelativeLayout>


【知识点】布局文件必须定义成RelativeLayout吗???

android:layout_alignParentBottom等属性必须在RelativeLayout中使用,LinearLayout中没有这个属性。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: