android的GridView和Gallery-创新互联
GridView:
创新互联长期为上千客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为乐清企业提供专业的成都网站建设、成都做网站,乐清网站改版等技术服务。拥有十多年丰富建站经验和众多成功案例,为您定制开发。activity_main.xml
MainActivity
package com.example.gridview; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; public class MainActivity extends Activity { private GridView gridView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gridView=(GridView) findViewById(R.id.gridView1_1); MyImageAdapter myImageAdapter=new MyImageAdapter(this); gridView.setAdapter(myImageAdapter); } @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; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } static class MyImageAdapter extends BaseAdapter{ private Context ct; //要显示的图片资源 private int [] p_w_picpaths={ R.drawable.th_seismometer_1, R.drawable.th_skippylite, R.drawable.th_sms_hey_blue, R.drawable.th_ssh, R.drawable.th_things1, R.drawable.th_thisday, R.drawable.th_seismometer_1, R.drawable.th_skippylite, R.drawable.th_sms_hey_blue, R.drawable.th_ssh, R.drawable.th_things1, R.drawable.th_thisday}; MyImageAdapter(Context ct){ this.ct=ct; } @Override public int getCount() { // TODO Auto-generated method stub return p_w_picpaths.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 p_w_picpathView; if(convertView==null){ p_w_picpathView=new ImageView(ct); //设置图片的宽和高 p_w_picpathView.setLayoutParams(new GridView.LayoutParams(85, 85)); //设置拉伸或截取方式 p_w_picpathView.setScaleType(ImageView.ScaleType.CENTER_CROP); p_w_picpathView.setPadding(8, 8, 8, 8); }else{ p_w_picpathView=(ImageView)convertView; } p_w_picpathView.setImageResource(p_w_picpaths[position]); return p_w_picpathView; } } }
Gallery:
activity_main.xml
MainActivity
package com.example.gallery; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; public class MainActivity extends Activity { private Gallery gallery; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gallery=(Gallery) findViewById(R.id.gallery1_1); MyGalleryAdapter myGalleryAdapter=new MyGalleryAdapter(); gallery.setAdapter(myGalleryAdapter); } @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; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } class MyGalleryAdapter extends BaseAdapter{ private int [] p_w_picpaths={ R.drawable.th_seismometer_1, R.drawable.th_skippylite, R.drawable.th_sms_hey_blue, R.drawable.th_ssh, R.drawable.th_things1, R.drawable.th_thisday, R.drawable.th_seismometer_1, R.drawable.th_skippylite, R.drawable.th_sms_hey_blue, R.drawable.th_ssh, R.drawable.th_things1, R.drawable.th_thisday}; @Override public int getCount() { // TODO Auto-generated method stub return p_w_picpaths.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 p_w_picpathView; if(convertView==null){ p_w_picpathView=new ImageView(MainActivity.this); }else{ p_w_picpathView=(ImageView)convertView; } p_w_picpathView.setImageResource(p_w_picpaths[position]); return p_w_picpathView; } } }
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
网站标题:android的GridView和Gallery-创新互联
当前地址:http://ybzwz.com/article/cedids.html