安卓学习第17课——Gallery
生活随笔
收集整理的這篇文章主要介紹了
安卓学习第17课——Gallery
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
雖然Gallery已經(jīng)過時了,但是既然書上講了,我還要學(xué)習(xí)一下。。產(chǎn)生的效果很好。。。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent" android:orientation="vertical"><ImageViewandroid:id="@+id/imageView"android:layout_width="320dp"android:layout_height="320dp"/><Galleryandroid:id="@+id/gallery"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="25dp"android:unselectedAlpha="0.6"android:spacing="2pt" /> </LinearLayout> <?xml version="1.0" encoding="utf-8"?> <resources><declare-styleable name="Gallery"><attr name="android:galleryItemBackground" /></declare-styleable> </resources> package com.example.gallery; import android.app.Activity; import android.content.res.TypedArray; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView;@SuppressWarnings("deprecation") public class MainActivity extends Activity {int[] imageIds=new int[]{R.drawable.shuangzi,R.drawable.shuangyu,R.drawable.chunv,R.drawable.tiancheng,R.drawable.tianxie,R.drawable.sheshou,R.drawable.juxie,R.drawable.shuiping,R.drawable.shizi,R.drawable.baiyang,R.drawable.jinniu,R.drawable.mojie};Gallery gallery;ImageView imageView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); gallery=(Gallery) findViewById(R.id.gallery); imageView=(ImageView) findViewById(R.id.imageView); BaseAdapter baseAdapter=new BaseAdapter(){@Overridepublic int getCount() {return imageIds.length;}@Overridepublic Object getItem(int position) {return position;}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) { ImageView imageView=new ImageView(MainActivity.this);imageView.setImageResource(imageIds[position]);imageView.setScaleType(ImageView.ScaleType.FIT_XY);imageView.setLayoutParams(new Gallery.LayoutParams(75,100));TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);imageView.setBackgroundResource(typedArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0));return imageView;}}; gallery.setAdapter(baseAdapter); gallery.setOnItemSelectedListener(new OnItemSelectedListener(){@Overridepublic void onItemSelected(AdapterView<?> parent, View view, int position,long id) {imageView.setImageResource(imageIds[position]);}@Overridepublic void onNothingSelected(AdapterView<?> parent) {}});}}轉(zhuǎn)載于:https://www.cnblogs.com/Yvettey-me/p/3932866.html
總結(jié)
以上是生活随笔為你收集整理的安卓学习第17课——Gallery的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (转)jQuery.fn.extend与
- 下一篇: Axure教程:如何使用动态面板?动态面