一. 下拉列表
二 . 输入内容自动完成文本框
三. 可展开的多级列表
(ExpandableListView)
四. Gallery(画廊)
五. Silding 效果抽屉
六. Popupwindow 弹出窗口
---------------效果图--------------
image.png
[图片上传中...(image.png-a4c697-1526465903460-0)]
image.png
xml------------中 定义一个Spinner
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
-----------MainActivity-----------中---
···
spinner=(Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(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);
···
输入内容自动完成文本框
-------------xml--------------
<AutoCompleteTextView
android:id="@+id/at"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
/>
-------------------MainActivity-----------中-------------
at=(AutoCompleteTextView) findViewById(R.id.at);
String name[]={"12","1233","234","2356","2333","456","4569","45698","7899"};
ArrayAdapter<String> aa=new ArrayAdapter<String>
(this, android.R.layout.simple_spinner_dropdown_item,name);
at.setAdapter(aa);
效果图**********************
image.png
可展开的多级列表*****************
ExpandableListView
************效果图************
image.png
------------MainActivity--------中代码如下-----------
···
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;
@Override
protected 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() {
@Override
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() {
@Override
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;
}
@Override//获取指定分组的指定子选项的视图
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
ChildViewHolder childHolder=null;
if (convertView==null) {
convertView=View.inflate(MainActivity.this, R.layout.item_expand_child, null);
childHolder=new ChildViewHolder();
childHolder.tvTitle=(TextView) convertView.findViewById(R.id.label_expand_child);
convertView.setTag(childHolder);
} else {
childHolder=(ChildViewHolder) convertView.getTag();
}
childHolder.tvTitle.setText(childStrings[groupPosition][childPosition]);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
class GroupViewHolder {
TextView tvTitle;
}
class ChildViewHolder {
TextView tvTitle;
}
}
}
···
我们自定义两个布局 一个Group.xml
一个 child .xml 布局
在activity_main.xml中
···
<ExpandableListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/expand_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ExpandableListView>
···
-------------child.xml---------------
···
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/label_expand_child"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="20dp"
android:textColor="@android:color/white"
android:gravity="center"
android:background="@android:color/darker_gray"
android:textSize="20sp" />
-----------group.xml-------------
···
···
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/label_expand_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@android:color/holo_blue_light"
android:paddingLeft="20dp"
android:textColor="@android:color/white"
android:textSize="20sp" />
···
***********效果图***********
image.png
可以滑动
我们先定义一个GalleryAdapter类存储信息
-----------------代码如下---------------------
···
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;
}
@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;
}
}
···
在布局中定义一个Gallery
xml-------------------代码如下-------------
···
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.mytest.MainActivity">
<Gallery
android:id="@+id/gallery"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
···
**************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;
@Override
protected 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();
}
});
}
}
···
*******************抽屉效果***图*************
image.png
image.png
自定义一个MyGridViewAdapter保存信息---------
***********代码如下**************
···
package com.example.test11_silding;
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) { //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;
}
}
···
************MainActivity*************
···
package com.example.test11_silding;
import android.app.Activity;
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.widget01, R.drawable.widget02,
R.drawable.widget03, R.drawable.widget04,
R.drawable.widget05, R.drawable.widget06 ,
R.drawable.widget07,R.drawable.widget08 ,
R.drawable.widget09
};
private String[] items={
"手机防盗","通讯卫士","软件管理","流量管理","进程管理","手机杀毒","22","33","44"
};
@Override
protected 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);
}
});
}
}
···
自定义一个grd.xml************代码如下
···
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
/>
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dip"
/>
</LinearLayout>
···
main.xml中*******************
···
<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"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="抽屉效果"
android:textSize="20sp" />
<SlidingDrawer
android:id="@+id/sliding_drawer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:content="@+id/mycontent"
android:handle="@+id/layout1"
android:orientation="horizontal" >
<LinearLayout
android:id="@id/layout1"
android:layout_width="35px"
android:layout_height="fill_parent"
android:gravity="center"
android:background="#00000000"
>
<ImageView
android:id="@+id/my_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/left1"
/>
</LinearLayout>
<GridView
android:id="@id/mycontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20dip"
android:numColumns="3"
android:gravity="center"
android:background="#ff000000"
/>
</SlidingDrawer>
</RelativeLayout>
···
PopupWindow ************弹出窗口****
*************效果图*************
定义一个布局 ---popup.xml--------------
···
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bg"
>
<GridView
android:id="@+id/gv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="4"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
>
</GridView>
</LinearLayout>
···
一个Item.xml*********
···
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
<ImageView
android:id="@+id/iv_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/i1"
/>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="搜索"
android:textColor="@android:color/darker_gray"
/>
</LinearLayout>
···
activity_main.xml******代码如下********
···
<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"
tools:context="${relativePackage}.${activityClass}" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示"
android:onClick="show"
/>
</RelativeLayout>
···
实现Maintivty中
*************代码如下**************
···
package com.example.test12_popupwindiow;
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.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;
@Override
protected 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() {
@Override//点击泡泡 条目消失
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
pw.dismiss();
}
});
pw=new PopupWindow(view,
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
/**
*
* 如果需要点击其他的地方泡泡自动消失的两个条件
*1.泡泡必须指定背景
*
*2.必须指定获取焦点
* */
pw.setBackgroundDrawable(new BitmapDrawable());
pw.setFocusable(true);
//显示
// pw.showAsDropDown(v);//指定在控件(按钮)的下方出现
//显示在屏幕下发
//parent v是按钮,v.getParent()是布局 Gravity是显示 在布局中的位置
//水平偏移量
//垂直偏移量
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[]{
"搜索","2","3","4","5","6","7","8"
};
@Override
public int getCount() {
return names.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return names[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView==null) {
convertView=View.inflate(MainActivity.this,
R.layout.item, null);
ImageView iv= (ImageView) convertView.findViewById(R.id.iv_icon);
TextView tv= (TextView) convertView.findViewById(R.id.tv);
iv.setImageResource(images[position]);
tv.setText(names[position]);
}
return convertView;
}
}
}
···
网友评论