组件

package com.example.text12;import android.app.Activity;import android.graphics.drawable.BitmapDrawable;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.view.ViewGroup;import android.widget.Adapter;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.BaseAdapter;import android.widget.GridView;import android.widget.ImageView;import android.widget.PopupWindow;import android.widget.TextView;public class MainActivity extends Activity {private PopupWindow pw;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void show(View v){View view = View.inflate(this, R.layout.popup, null);GridView gv =(GridView) view.findViewById(R.id.gv);gv.setAdapter(new MyAdapter());gv.setOnItemClickListener(new OnItemClickListener() {public void onItemClick(AdapterView?parent,View view,int position,long id){}});pw = new PopupWindow(view,ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);pw.setBackgroundDrawable(new BitmapDrawable());pw.setFocusable(true);//显示//pw.showAsDropDown(v);pw.showAtLocation((View)v.getParent(), Gravity.BOTTOM, 0, 0);}class MyAdapter extends BaseAdapter{private int [] images =new int[]{R.drawable.i1,R.drawable.i2,R.drawable.i3,R.drawable.i4,R.drawable.i5,R.drawable.i6,R.drawable.i7,R.drawable.i8,};private String [] names = new String[]{"搜索管理","文件管理","下载管理","全屏管理","网址管理","书签管理","界面管理","加入界面"};@Overridepublic int getCount() {// TODO Auto-generated method stubreturn names.length;}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn names;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubif(convertView == null){convertView = View.inflate(MainActivity.this,R.layout.item, null);}ImageView iv_icon = (ImageView) convertView.findViewById(R.id.iv_icon);TextView iv_name = (TextView) convertView.findViewById(R.id.iv_name);iv_icon.setImageResource(images[position]);iv_name.setText(names[position]);return convertView;}}}?xml version="1.0" encoding="utf-8"?



package com.example.text11;import android.app.Activity;import android.content.res.Configuration;import android.os.Bundle;import android.widget.GridView;import android.widget.ImageView;import android.widget.SlidingDrawer;public class MainActivity extends Activity {private GridView gridView; private SlidingDrawer slidingDrawer; private ImageView imageView; private int[] icons={ R.drawable.f1, R.drawable.f2, R.drawable.f3, R.drawable.f4, R.drawable.f5, R.drawable.f6, R.drawable.f7, R.drawable.f8, R.drawable.f9 }; private String[] items={ "Phone", "Message", "AddImage", "Music", "Telephone", "SMS" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gridView = (GridView)findViewById(R.id.mycontent); slidingDrawer = (SlidingDrawer)findViewById(R.id.sliding_drawer); imageView = (ImageView)findViewById(R.id.my_image); MyGridViewAdapter adapter = new MyGridViewAdapter(this, items, icons); gridView.setAdapter(adapter); slidingDrawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() { public void onDrawerOpened() { imageView.setImageResource(R.drawable.right1); } }); slidingDrawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() { public void onDrawerClosed() { imageView.setImageResource(R.drawable.left1); } }); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); } }package com.example.text11;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;public class MyGridViewAdapter extends BaseAdapter{ private Context context; private String[] items; private int[] icons; public MyGridViewAdapter(Context context, String[] items, int[] icons){ this.context = context; this.items = items; this.icons = icons; } public int getCount() { return items.length; } public Object getItem(int arg0) { return items[arg0]; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater layoutInflater = LayoutInflater.from(context); View view = (View)layoutInflater .inflate(R.layout.grid, null); ImageView imageView = (ImageView)view.findViewById(R.id.image_view); TextView textview = (TextView)view.findViewById(R.id.text_view); imageView.setImageResource(icons[position]); textview.setText(items[position]); return view; } } 


package com.example.text10;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;/** * Created by ZWH on 2016/5/16. */public class GalleryAdapter extends BaseAdapter { private Context mContext; //设置要展示的图片资源 int[] images = {R.drawable.f1, R.drawable.f2, R.drawable.f3, R.drawable.f4}; public GalleryAdapter(Context context) { this.mContext = context; } @Override public int getCount() { return images.length; } @Override public Object getItem(int i) { return i; } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { //在此最好判断一下view是否为空 ImageView image = new ImageView(mContext); image.setImageResource(images[i]); image.setAdjustViewBounds(true);//设置调整视图的边界 //设置宽高 image.setLayoutParams(new Gallery.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); return image; }}package com.example.text10;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.Gallery;import android.widget.Toast;public class MainActivity extends Activity {GalleryAdapter galleryAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Gallery gallery = (Gallery) findViewById(R.id.gallery); galleryAdapter = new GalleryAdapter(MainActivity.this); gallery.setAdapter(galleryAdapter); //相应的点击事件 gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView?adapterView, View view, int i, long l) { Toast.makeText(MainActivity.this, "您点击的是" + i, Toast.LENGTH_LONG).show(); } }); }}?xml version="1.0" encoding="utf-8"?


package com.example.app07_en;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.BaseExpandableListAdapter;import android.widget.ExpandableListView;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {public String[] groupStrings = {"西游记", "水浒传", "三国演义", "红楼梦"};public String[][] childStrings = {{"唐三藏", "孙悟空", "猪八戒", "沙和尚"},{"宋江", "林冲", "李逵", "鲁智深"},{"曹操", "刘备", "孙权", "诸葛亮", "周瑜"},{"贾宝玉", "林黛玉", "薛宝钗", "王熙凤"}};private ExpandableListView expand_list;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);expand_list=(ExpandableListView) findViewById(R.id.expand_list);MyExpandablelistAdapter adapater = new MyExpandablelistAdapter(); expand_list.setAdapter(adapater); //点击组的监听expand_list.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { Toast.makeText(getApplicationContext(), groupStrings[groupPosition], Toast.LENGTH_SHORT).show();return false;//务必返回false,否则分组不会展开 } });//设置子选项监听expand_list.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText(getApplicationContext(), childStrings[groupPosition][childPosition], Toast.LENGTH_SHORT).show(); return false; } });}class MyExpandablelistAdapter extends BaseExpandableListAdapter{ @Override//获取分组的个数public int getGroupCount() { return groupStrings.length; } @Override//获取指定分组的子选项的个数 public int getChildrenCount(int groupPosition) { return childStrings[groupPosition].length; } @Override//获取指定的分组数据 根据下标获取 public Object getGroup(int groupPosition) { return groupStrings[groupPosition]; } @Override//获取指定分组中指定的子选项的数据public Object getChild(int groupPosition, int childPosition) { return childStrings[groupPosition][childPosition]; } @Override//获取指定分组的id public long getGroupId(int groupPosition) { return groupPosition; }@Override//获取指定分组的子选项的id public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override//分组和子选项是否持有稳定的id 也就是底层数据的改变不会影响到它俩 public boolean hasStableIds() { return true; } @Override//获取指定分组的视图 public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { GroupViewHolder grouoholder=null; if (convertView==null) { convertView=View.inflate(MainActivity.this, R.layout.item_expand_group, null); grouoholder=new GroupViewHolder(); grouoholder.tvTitle=(TextView)convertView.findViewById(R.id.label_expand_group);convertView.setTag(grouoholder); } else { grouoholder=(GroupViewHolder) convertView.getTag(); } grouoholder.tvTitle.setText(groupStrings[groupPosition]); return convertView; }我们自定义两个布局 一个Group.xml一个 child .xml 布局在activity_main.xml中······-------------child.xml---------------···?xml version="1.0" encoding="utf-8"?


package com.example.test10_gallery;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;public class GalleryAdapter extends BaseAdapter {private Context mContext;//设置要展示的图片资源int[] images = {R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher};public GalleryAdapter(Context context) { this.mContext = context;}@Overridepublic int getCount() { return images.length;}@Overridepublic Object getItem(int i) { return i;}@Overridepublic long getItemId(int i) { return i;}@Overridepublic View getView(int i, View view, ViewGroup viewGroup) { //在此最好判断一下view是否为空 ImageView image = new ImageView(mContext); image.setImageResource(images[i]); image.setAdjustViewBounds(true); //设置宽高image.setLayoutParams(new Gallery.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); return image;}在布局中定义一个Gallery···?xml version="1.0" encoding="utf-8"?···**************MainActivity*************中代码如下----------···package com.example.test10_gallery;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.Gallery;import android.widget.Toast;public class MainActivity extends Activity {GalleryAdapter galleryAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Gallery gallery = (Gallery) findViewById(R.id.gallery);



android:id="@+id/spinner"android:layout_width="match_parent"android:layout_height="wrap_content"spinner=(Spinner) findViewById(R.id.spinner); ArrayAdapteradapter = new ArrayAdapter (this, android.R .layout .simple_spinner_item); //系统自带的下拉 adapter.setDropDownViewResource(android.R .layout.simple_spinner_dropdown_item); //下拉 //添加选项 adapter.add("java"); adapter.add("C++"); adapter.add("C#"); adapter.add("Python"); spinner.setAdapter(adapter);


android:id="@+id/at" android:layout_width="match_parent"android:layout_height="wrap_content" android:completionThreshold="1" />String name[]={"12","1233","234","2356","2333","456","4569","45698","7899"}; ArrayAdapteraa=new ArrayAdapter (this, android.R.layout.simple_spinner_dropdown_item,name); at.setAdapter(aa);

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 210,978评论 6 490
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 89,954评论 2 384
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 156,623评论 0 345
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,324评论 1 282
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,390评论 5 384
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,741评论 1 289
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,892评论 3 405
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,655评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,104评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,451评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,569评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,254评论 4 328
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,834评论 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,725评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,950评论 1 264
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,260评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,446评论 2 348

推荐阅读更多精彩内容