源码地址:http://download.csdn.net/detail/u014608640/7298189 不要分的哦
方法一layout:
public void init(){ ImageView popimg=(ImageView) findViewById(R.id.pop_img); popimg.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub LinearLayout layout = new LinearLayout(ShowPopwindow.this); layout.setBackgroundColor(Color.GRAY); TextView tv = new TextView(ShowPopwindow.this); tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); tv.setText("I‘m a pop -----------------------------!"); tv.setTextColor(Color.WHITE); layout.addView(tv); // 设置相关属性 popwindow = new PopupWindow(layout,w,h); // 设置动画效果 popwindow.setFocusable(true); popwindow.setOutsideTouchable(true); popwindow.setBackgroundDrawable(new BitmapDrawable()); popwindow.showAsDropDown(et, 0, 1); // int[] location = new int[2]; // v.getLocationOnScreen(location); // popwindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1]-popwindow.getHeight()); } }); }
方法二自定义布局:
public void initPopuptWindow(){ ImageView popimg=(ImageView) findViewById(R.id.pop_img); popimg.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub LayoutInflater inflater = (LayoutInflater)ShowPopwindow.this .getSystemService(LAYOUT_INFLATER_SERVICE); View v1 = inflater.inflate(R.layout.pop_listview, null); EditText et1 = (EditText) v1.findViewById(R.id.et1); w=et.getWidth(); h=et.getHeight(); Log.e("INFO", w+"==="+h); // 设置相关属性 popwindow = new PopupWindow(v1,w,h); // 设置动画效果 popwindow.setFocusable(true); popwindow.setOutsideTouchable(true); popwindow.setBackgroundDrawable(new BitmapDrawable()); popwindow.showAsDropDown(et, 0, 1); } }); }
XML代码:
<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/login_input_bg" android:gravity="center_vertical" android:padding="0.0dip" android:layout_margin="15.0dip" > <TextView android:id="@+id/tx" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="车架号" android:textColor="#f00" android:layout_centerVertical="true" android:padding="10.0dip" /> <EditText android:id="@+id/lc" android:layout_width="match_parent" android:layout_height="43.0dip" android:layout_toRightOf="@id/tx" android:background="@null" /> <ImageView android:id="@+id/pop_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/bg_pull2" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:padding="10.0dip" /> </RelativeLayout>
代码完毕!
android之popwindow操作
时间: 2024-10-10 07:28:41