现在很多的应用效果都需要做的炫些,像UC,以及天天静听,效果很炫的,源码已经对外开放了,有兴趣的可以去研究下的
上源码
main.xml
01 |
<? xml version = "1.0" encoding = "utf-8" ?> |
02 |
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
03 |
android:layout_width = "fill_parent" |
04 |
android:layout_height = "500dp" |
05 |
android:orientation = "vertical" |
06 |
android:background = "<a href=" http://my.oschina.net/asia" rel = "nofollow" target = "_blank" >@android</ a > :color/white" |
07 |
> |
08 |
09 |
< include android:id = "@+id/top_title" |
10 |
|
11 |
layout = "@layout/title_two_button" /> |
12 |
< Button |
13 |
android:id = "@+id/bb" |
14 |
android:layout_width = "fill_parent" |
15 |
android:layout_height = "wrap_content" |
16 |
android:text = "button" |
17 |
/> |
18 |
</ LinearLayout > |
01 |
<? xml version = "1.0" encoding = "utf-8" ?> |
02 |
< FrameLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
03 |
android:layout_width = "fill_parent" |
04 |
android:layout_height = "wrap_content" > |
05 |
06 |
< TextView |
07 |
android:id = "@+id/tvtitle" |
08 |
android:layout_width = "fill_parent" |
09 |
android:layout_height = "45dip" |
10 |
android:background = "@drawable/search_bg" |
11 |
android:gravity = "center" |
12 |
android:text = "天下第一" |
13 |
android:textColor = "@color/black" |
14 |
android:textSize = "25dip" /> |
15 |
16 |
< Button |
17 |
android:id = "@+id/btn_title_left" |
18 |
android:layout_width = "49dip" |
19 |
android:layout_height = "36dip" |
20 |
android:layout_gravity = "center_vertical" |
21 |
android:background = "@drawable/title_button" |
22 |
android:textColor = "@color/white" /> |
23 |
24 |
< Button |
25 |
android:id = "@+id/btn_title_right" |
26 |
android:layout_width = "49dip" |
27 |
android:layout_height = "36dip" |
28 |
android:layout_gravity = "right" |
29 |
android:layout_marginRight = "2dip" |
30 |
android:layout_marginTop = "4dip" |
31 |
android:background = "@drawable/title_button2" |
32 |
android:textColor = "@color/white" /> |
33 |
34 |
</ FrameLayout > |
01 |
<? xml version = "1.0" encoding = "utf-8" ?> |
02 |
< LinearLayout android:orientation = "vertical" android:paddingLeft = "0.0sp" android:paddingRight = "0.0sp" android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:layout_margin = "0.0px" |
03 |
xmlns:android = "http://schemas.android.com/apk/res/android" |
04 |
android:background = "@drawable/group_bg" > |
05 |
06 |
< ImageView |
07 |
android:id = "@+id/iv_group_list_bg_divider" |
08 |
android:background = "@drawable/group_divider" |
09 |
android:padding = "0.0px" |
10 |
android:layout_width = "fill_parent" |
11 |
android:layout_height = "wrap_content" |
12 |
android:layout_margin = "0.0px" /> |
13 |
< ListView |
14 |
android:id = "@+id/lvGroup" |
15 |
android:fadingEdgeLength = "0.0sp" |
16 |
android:layout_width = "fill_parent" |
17 |
android:layout_height = "fill_parent" |
18 |
android:listSelector = "@drawable/grouplist_item_bg" |
19 |
android:drawSelectorOnTop = "true" |
20 |
android:cacheColorHint = "#00000000" |
21 |
android:divider = "@drawable/group_divider" |
22 |
android:dividerHeight = "2.0px" |
23 |
android:layout_weight = "0.0" /> |
24 |
</ LinearLayout > |
01 |
<? xml version = "1.0" encoding = "utf-8" ?> |
02 |
< LinearLayout android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "@dimen/group_item_height" |
03 |
xmlns:android = "http://schemas.android.com/apk/res/android" > |
04 |
< TextView |
05 |
android:gravity = "center" |
06 |
android:id = "@+id/groupItem" |
07 |
android:layout_width = "fill_parent" |
08 |
android:layout_height = "fill_parent" |
09 |
style = "@style/grouplist_item_textview" /> |
10 |
</ LinearLayout > |
001 |
package jk.activity; |
002 |
003 |
import java.util.ArrayList; |
004 |
import java.util.List; |
005 |
006 |
007 |
008 |
import android.app.Activity; |
009 |
import android.content.Context; |
010 |
import android.graphics.drawable.BitmapDrawable; |
011 |
import android.os.Bundle; |
012 |
import android.util.Log; |
013 |
import android.view.LayoutInflater; |
014 |
import android.view.View; |
015 |
import android.view.WindowManager; |
016 |
import android.widget.AdapterView; |
017 |
import android.widget.AdapterView.OnItemClickListener; |
018 |
import android.widget.ArrayAdapter; |
019 |
import android.widget.Button; |
020 |
import android.widget.ListView; |
021 |
import android.widget.PopupWindow; |
022 |
import android.widget.TextView; |
023 |
import android.widget.Toast; |
024 |
025 |
public class PoupWindowDemoActivity extends Activity { |
026 |
027 |
private PopupWindow popupWindow; |
028 |
029 |
private ListView lv_group; |
030 |
031 |
private View view; |
032 |
033 |
private View top_title; |
034 |
035 |
private TextView tvtitle; |
036 |
037 |
private List<String> groups; |
038 |
|
039 |
private Button button; |
040 |
public void onCreate(Bundle savedInstanceState) { |
041 |
super .onCreate(savedInstanceState); |
042 |
setContentView(R.layout.main); |
043 |
button = (Button)findViewById(R.id.bb); |
044 |
top_title = this .findViewById(R.id.top_title); |
045 |
046 |
tvtitle = (TextView) top_title.findViewById(R.id.tvtitle); |
047 |
048 |
tvtitle.setText( "天下第一" ); |
049 |
050 |
tvtitle.setOnClickListener( new View.OnClickListener() { |
051 |
052 |
@Override |
053 |
public void onClick(View v) { |
054 |
showWindow(v); |
055 |
} |
056 |
}); |
057 |
058 |
} |
059 |
060 |
/** |
061 |
* 显示 |
062 |
* |
063 |
* @param parent |
064 |
*/ |
065 |
private void showWindow(View parent) { |
066 |
067 |
if (popupWindow == null ) { |
068 |
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
069 |
070 |
view = layoutInflater.inflate(R.layout.group_list, null ); |
071 |
072 |
lv_group = (ListView) view.findViewById(R.id.lvGroup); |
073 |
// 加载数据 |
074 |
groups = new ArrayList<String>(); |
075 |
groups.add( "女儿国" ); |
076 |
groups.add( "西游" ); |
077 |
groups.add( "大话" ); |
078 |
groups.add( "大唐" ); |
079 |
080 |
GroupAdapter groupAdapter = new GroupAdapter( this , groups); |
081 |
lv_group.setAdapter(groupAdapter); |
082 |
// 创建一个PopuWidow对象 |
083 |
popupWindow = new PopupWindow(view, 200 , 250 ); |
084 |
} |
085 |
086 |
// 使其聚集 |
087 |
popupWindow.setFocusable( true ); |
088 |
// 设置允许在外点击消失 |
089 |
popupWindow.setOutsideTouchable( true ); |
090 |
091 |
// 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景 |
092 |
popupWindow.setBackgroundDrawable( new BitmapDrawable()); |
093 |
WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); |
094 |
// 显示的位置为:屏幕的宽度的一半-PopupWindow的高度的一半 |
095 |
int xPos = windowManager.getDefaultDisplay().getWidth() / 2 |
096 |
- popupWindow.getWidth() / 2 ; |
097 |
098 |
Log.i( "coder" , "windowManager.getDefaultDisplay().getWidth()/2:" |
099 |
+ windowManager.getDefaultDisplay().getWidth() / 2 ); |
100 |
// |
101 |
Log.i( "coder" , "popupWindow.getWidth()/2:" + popupWindow.getWidth() / 2 ); |
102 |
103 |
Log.i( "coder" , "xPos:" + xPos); |
104 |
105 |
popupWindow.showAsDropDown(parent, xPos, 0 ); |
106 |
107 |
lv_group.setOnItemClickListener( new OnItemClickListener() { |
108 |
109 |
@Override |
110 |
public void onItemClick(AdapterView<?> adapterView, View view, |
111 |
int position, long id) { |
112 |
113 |
Toast.makeText(PoupWindowDemoActivity. this , |
114 |
"groups.get(position)" + groups.get(position), 1000 ) |
115 |
.show(); |
116 |
117 |
if (popupWindow != null ) { |
118 |
popupWindow.dismiss(); |
119 |
} |
120 |
} |
121 |
}); |
122 |
} |
123 |
} |
01 |
package jk.activity; |
02 |
03 |
import java.util.List; |
04 |
05 |
06 |
07 |
import android.content.Context; |
08 |
import android.view.LayoutInflater; |
09 |
import android.view.View; |
10 |
import android.view.ViewGroup; |
11 |
import android.widget.BaseAdapter; |
12 |
import android.widget.TextView; |
13 |
14 |
public class GroupAdapter extends BaseAdapter { |
15 |
16 |
private Context context; |
17 |
18 |
private List<String> list; |
19 |
20 |
public GroupAdapter(Context context, List<String> list) { |
21 |
22 |
this .context = context; |
23 |
this .list = list; |
24 |
25 |
} |
26 |
27 |
@Override |
28 |
public int getCount() { |
29 |
return list.size(); |
30 |
} |
31 |
32 |
@Override |
33 |
public Object getItem( int position) { |
34 |
35 |
return list.get(position); |
36 |
} |
37 |
38 |
@Override |
39 |
public long getItemId( int position) { |
40 |
return position; |
41 |
} |
42 |
43 |
@Override |
44 |
public View getView( int position, View convertView, ViewGroup viewGroup) { |
45 |
46 |
|
47 |
ViewHolder holder; |
48 |
if (convertView== null ) { |
49 |
convertView=LayoutInflater.from(context).inflate(R.layout.group_item_view, null ); |
50 |
holder= new ViewHolder(); |
51 |
|
52 |
convertView.setTag(holder); |
53 |
|
54 |
holder.groupItem=(TextView) convertView.findViewById(R.id.groupItem); |
55 |
|
56 |
} |
57 |
else { |
58 |
holder=(ViewHolder) convertView.getTag(); |
59 |
} |
60 |
holder.groupItem.setText(list.get(position)); |
61 |
|
62 |
return convertView; |
63 |
} |
64 |
65 |
static class ViewHolder { |
66 |
TextView groupItem; |
67 |
} |
68 |
69 |
} |
对于adapter,最重要是getView(int position, View convertView, ViewGroup viewGroup)这个方法的,菜已上完,各位慢慢享用