android用户界面-组件Widget-画廊视图Gallery
16lz
2022-04-10
Android的Gallery控件是个很不错的看图控件,大大减轻了开发者对于看图功能的开发,而且效果也比较美观。
/Chapter04_UI_Gallery01/src/com/amaker/test/MainActivity.java
- 代码
- packagecom.amaker.test;
- importandroid.app.Activity;
- importandroid.content.Context;
- importandroid.os.Bundle;
- importandroid.view.View;
- importandroid.view.ViewGroup;
- importandroid.widget.BaseAdapter;
- importandroid.widget.Gallery;
- importandroid.widget.ImageView;
- publicclassMainActivityextendsActivity{
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- //创建时被调用
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Galleryg=(Gallery)findViewById(R.id.Gallery01);
- g.setAdapter(newMyAdapter(this));
- }
- classMyAdapterextendsBaseAdapter{
- Contextcontext;
- MyAdapter(Contextcontext){
- this.context=context;
- }
- privateInteger[]imgs={
- R.drawable.gallery_photo_1,
- R.drawable.gallery_photo_2,
- R.drawable.gallery_photo_3,
- R.drawable.gallery_photo_4,
- R.drawable.gallery_photo_5,
- R.drawable.gallery_photo_6,
- R.drawable.gallery_photo_7,
- R.drawable.gallery_photo_8,
- R.drawable.gallery_photo_1,
- R.drawable.gallery_photo_2,
- R.drawable.gallery_photo_3,
- R.drawable.gallery_photo_4,
- R.drawable.gallery_photo_5,
- R.drawable.gallery_photo_6,
- R.drawable.gallery_photo_7,
- R.drawable.gallery_photo_8
- };
- publicintgetCount(){
- returnimgs.length;
- }
- publicObjectgetItem(intitem){
- returnitem;
- }
- publiclonggetItemId(intid){
- returnid;
- }
- publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
- ImageViewimageView=newImageView(context);
- imageView.setImageResource(imgs[position]);
- imageView.setScaleType(ImageView.ScaleType.FIT_XY);
- imageView.setLayoutParams(newGallery.LayoutParams(136,88));
- returnimageView;
- }
- }
- }
/Chapter04_UI_Gallery01/res/layout/main.xml
- 代码
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Gallery
- android:id="@+id/Gallery01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- </Gallery>
- </LinearLayout>
更多相关文章
- Dagger-Android使用手册
- 整理出15个Android很有用的代码片段
- androidのMMS短信发送过程(一)
- Android从入门到放弃——常见控件二
- Android(安卓)发布可穿戴设备 SDK 的开发者预览版
- Android(安卓)常用代码集合
- Android(安卓)开发中遇到的 bug(6)
- 25个你应该阅读的Android网站
- android Launcher Demo小结