Gallery简单应用
2015-10-20 10:39
316 查看
首先要自定义一个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; } @Override public int getCount() { // TODO Auto-generated method stub return data.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return data[position]; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return data[position]; } @Override public 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" > <Gallery android: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 }; @Override protected 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)); } @Override public 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; } }
相关文章推荐
- A20串口的调试,特殊波特率
- eclipse一些问题点归纳整理
- hook对应的汇编码0x8B, 0xFF,0x55,0x8B, 0xEC, // mov ebp,esp
- 配置git for eclipse
- 电磁感应加热的多路技术
- 图像处理之应用篇-大米计数续
- html px em pt长度单位(像素 相对长度 点)知识(转)
- PERFORMANCE_SCHEMA数据库
- 从菜鸟成为数据科学家的 9步养成方案
- centos 7 修改默认运行级别
- ABAP Dump when&n…
- function module …
- Android:Material Design on Android Checklist!
- Android导入错误 Invalid project description
- 小朋友选大王问题
- 使用TortoiseGit提交项目到github
- AMI corpus download
- SAP 金额在表中的存储…
- Copy fields from …
- Fixed Value Append