清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
1:布局文件
<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" > <TextView android:id="@+id/tv_text_age" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/choolse_bg" android:text="不限" android:paddingLeft="12dp" android:textColor="#9d72d2" android:layout_centerInParent="true" android:gravity="center_vertical" /> </RelativeLayout>
package com.jackie.popupwindow; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener { private TextView tv_text_age; private PopupWindow pop; ListView listView = null; private List<String> datas; private SelectAgeAdapter mSelectAdapter; private LayoutInflater inflater; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); inflater = LayoutInflater.from(this); tv_text_age = (TextView) findViewById(R.id.tv_text_age); tv_text_age.setOnClickListener(this); } private void initData() { datas = new ArrayList<String>(); for(int i = 0; i < 50; i++){ datas.add("HelloWorld - " + i); } } @Override public void onClick(View v) { if(v.getId() == R.id.tv_text_age){ if(pop == null) { listView = new ListView(this); listView.setDividerHeight(1); listView.setBackgroundResource(R.drawable.kge_feek_bg); listView.setCacheColorHint(0x00000000); mSelectAdapter = new SelectAgeAdapter(); pop = new PopupWindow(listView, tv_text_age.getWidth(), LayoutParams.WRAP_CONTENT, true); } listView.setAdapter(mSelectAdapter); pop.setBackgroundDrawable(new ColorDrawable(Color.RED)); pop.showAsDropDown(tv_text_age, 0, 0); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { tv_text_age.setText(datas.get(position)); dismissPopWindow(); } }); } } public void dismissPopWindow() { if(pop != null && pop.isShowing()){ pop.dismiss(); } } class SelectAgeAdapter extends BaseAdapter { @Override public int getCount() { return datas != null&&datas.size() > 0 ? datas.size() : 0; } @Override public Object getItem(int position) { return datas.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { holder = new ViewHolder(); convertView = inflater.inflate(R.layout.item, null); holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.tv_name.setText(datas.get(position)); return convertView; } } class ViewHolder { TextView tv_name; } }