Gallery简单应用
生活随笔
收集整理的這篇文章主要介紹了
Gallery简单应用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先要自定義一個adapter
package com.example.gallerydemo;import android.content.Context; import android.graphics.Color; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; import android.widget.LinearLayout.LayoutParams;public class GalleryAdapter extends BaseAdapter {private Context mycon;private int[] data;public GalleryAdapter(Context mycon, int[] data) {this.mycon = mycon;this.data = data;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn data.length;}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn data[position];}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn data[position];}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ImageView img = new ImageView(mycon);img.setBackgroundColor(Color.BLACK);img.setImageResource(data[position]);//設置文件資源img.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));return img;}} <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><Galleryandroid:id="@+id/ga"android:layout_width="match_parent"android:layout_height="match_parent" /></RelativeLayout>布局文件
package com.example.gallerydemo;import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.Gallery;public class MainActivity extends Activity {private Gallery ga;private int[] data = { R.drawable.addpeople, R.drawable.ic_launcher,R.drawable.star_empty, R.drawable.star_full, R.drawable.tb,R.drawable.tb2 };@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ga = (Gallery) findViewById(R.id.ga);ga.setAdapter(new GalleryAdapter(MainActivity.this, data));}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu);return true;}}?
轉載于:https://www.cnblogs.com/84126858jmz/p/4894115.html
總結
以上是生活随笔為你收集整理的Gallery简单应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用fprintf()函数格式化输出至磁
- 下一篇: zigbee协议栈学习(二)