android之popwindow操作

 源码地址: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操作,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。