zoukankan      html  css  js  c++  java
  • popupwindow 模拟新浪、腾讯title弹框效果

    MainActivity.java外部引用 原始文档
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    package com.jj.popupwindow;
    
    import android.app.Activity;
    import android.graphics.drawable.BitmapDrawable;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.Window;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.LinearLayout;
    import android.widget.ListView;
    import android.widget.PopupWindow;
    
    /***
     * PopupWindow
     * 
     * @author zhangjia
     * 
     */
    public class MainActivity extends Activity {
    	private Button button;
    	private PopupWindow popupWindow;
    	private LinearLayout layout;
    	private ListView listView;
    	private String title[] = { "全部", "我的微博", "周边", "智能排版", "同学" };
    
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		requestWindowFeature(Window.FEATURE_NO_TITLE);
    		setContentView(R.layout.main);
    		button = (Button) findViewById(R.id.tv_title);
    		button.setOnClickListener(new OnClickListener() {
    			@Override
    			public void onClick(View v) {
    				button.getTop();
    				int y = button.getBottom() * 3 / 2;
    				int x = getWindowManager().getDefaultDisplay().getWidth() / 4;
    
    				showPopupWindow(x, y);
    			}
    		});
    	}
    
    	public void showPopupWindow(int x, int y) {
    		layout = (LinearLayout) LayoutInflater.from(MainActivity.this).inflate(
    				R.layout.dialog, null);
    		listView = (ListView) layout.findViewById(R.id.lv_dialog);
    		listView.setAdapter(new ArrayAdapter<String>(MainActivity.this,
    				R.layout.text, R.id.tv_text, title));
    
    		popupWindow = new PopupWindow(MainActivity.this);
    		popupWindow.setBackgroundDrawable(new BitmapDrawable());
    		popupWindow
    				.setWidth(getWindowManager().getDefaultDisplay().getWidth() / 2);
    		popupWindow.setHeight(300);
    		popupWindow.setOutsideTouchable(true);
    		popupWindow.setFocusable(true);
    		popupWindow.setContentView(layout);
    		// showAsDropDown会把里面的view作为参照物,所以要那满屏幕parent
    		// popupWindow.showAsDropDown(findViewById(R.id.tv_title), x, 10);
    		popupWindow.showAtLocation(findViewById(R.id.main), Gravity.LEFT
    				| Gravity.TOP, x, y);//需要指定Gravity,默认情况是center.
    
    		listView.setOnItemClickListener(new OnItemClickListener() {
    
    			@Override
    			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
    					long arg3) {
    				button.setText(title[arg2]);
    				popupWindow.dismiss();
    				popupWindow = null;
    			}
    		});
    	}
    
    }
    
  • 相关阅读:
    获取Windows DPI
    你的float用对了吗
    Overhead of a .NET array
    【原】灵活运用sessionStorage或者localStorage
    【总结】总结写了3个React页面后遇到的各种坑
    【学】AngularJS日记(4)- 过滤器的使用
    【学】AngularJS日记(3)- $apply(), run()方法
    【学】AngularJS日记(2)
    【学】AngularJS日记(1)
    【学】React的学习之旅7-官方例子总结
  • 原文地址:https://www.cnblogs.com/fx2008/p/3140138.html
Copyright © 2011-2022 走看看