先看效果......
现在很多的应用效果都需要做的炫些,像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" |
09 | <include android:id="@+id/top_title" |
11 | layout="@layout/title_two_button"/> |
14 | android:layout_width="fill_parent" |
15 | android:layout_height="wrap_content" |
title_two_button.xml
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" > |
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" |
13 | android:textColor="@color/black" |
14 | android:textSize="25dip" /> |
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" /> |
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" /> |
group_list.xml
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"> |
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" /> |
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" /> |
group_item.xml
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"> |
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" /> |
好了,这些都是其中用到的布局文件,开始重头戏
003 | import java.util.ArrayList; |
004 | import java.util.List; |
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; |
025 | public class PoupWindowDemoActivity extends Activity { |
027 | private PopupWindow popupWindow; |
029 | private ListView lv_group; |
033 | private View top_title; |
035 | private TextView tvtitle; |
037 | private List<String> groups; |
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); |
046 | tvtitle = (TextView) top_title.findViewById(R.id.tvtitle); |
048 | tvtitle.setText("天下第一"); |
050 | tvtitle.setOnClickListener(new View.OnClickListener() { |
053 | public void onClick(View v) { |
065 | private void showWindow(View parent) { |
067 | if (popupWindow == null) { |
068 | LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
070 | view = layoutInflater.inflate(R.layout.group_list, null); |
072 | lv_group = (ListView) view.findViewById(R.id.lvGroup); |
074 | groups = new ArrayList<String>(); |
080 | GroupAdapter groupAdapter = new GroupAdapter(this, groups); |
081 | lv_group.setAdapter(groupAdapter); |
083 | popupWindow = new PopupWindow(view, 200, 250); |
087 | popupWindow.setFocusable(true); |
089 | popupWindow.setOutsideTouchable(true); |
092 | popupWindow.setBackgroundDrawable(new BitmapDrawable()); |
093 | WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); |
095 | int xPos = windowManager.getDefaultDisplay().getWidth() / 2 |
096 | - popupWindow.getWidth() / 2; |
098 | Log.i("coder", "windowManager.getDefaultDisplay().getWidth()/2:" |
099 | + windowManager.getDefaultDisplay().getWidth() / 2); |
101 | Log.i("coder", "popupWindow.getWidth()/2:" + popupWindow.getWidth() / 2); |
103 | Log.i("coder", "xPos:" + xPos); |
105 | popupWindow.showAsDropDown(parent, xPos, 0); |
107 | lv_group.setOnItemClickListener(new OnItemClickListener() { |
110 | public void onItemClick(AdapterView<?> adapterView, View view, |
111 | int position, long id) { |
113 | Toast.makeText(PoupWindowDemoActivity.this, |
114 | "groups.get(position)" + groups.get(position), 1000) |
117 | if (popupWindow != null) { |
118 | popupWindow.dismiss(); |
在这里还需要用到adapter,用到较多一般有baseadapter,simpleadapter,baseadapter挺强大的,不过simpleadapter扩展性挺不错的,可以满足自定义的需求,如果你理解输入输出流很快就能理解adapter的,他就是装备数据的
03 | import java.util.List; |
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; |
14 | public class GroupAdapter extends BaseAdapter { |
16 | private Context context; |
18 | private List<String> list; |
20 | public GroupAdapter(Context context, List<String> list) { |
22 | this.context = context; |
28 | public int getCount() { |
33 | public Object getItem(int position) { |
35 | return list.get(position); |
39 | public long getItemId(int position) { |
44 | public View getView(int position, View convertView, ViewGroup viewGroup) { |
48 | if (convertView==null) { |
49 | convertView=LayoutInflater.from(context).inflate(R.layout.group_item_view, null); |
50 | holder=new ViewHolder(); |
52 | convertView.setTag(holder); |
54 | holder.groupItem=(TextView) convertView.findViewById(R.id.groupItem); |
58 | holder=(ViewHolder) convertView.getTag(); |
60 | holder.groupItem.setText(list.get(position)); |
65 | static class ViewHolder { |
对于adapter,最重要是getView(int position, View convertView, ViewGroup viewGroup)这个方法的,菜已上完,各位慢慢享用