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);