• Android popupwindow 演示样例程序一


    经过多番測试实践,实现了popupwindow 弹出在指定控件的下方。代码上有凝视。有须要注意的地方。popupwindow 有自已的布局,里面控件的监听实现都有。接下来看代码实现。

    项目资源下载:点击这里

    TestPopwindow2.class

    package com.example.popwindowdemo;
    
    import android.app.ActionBar.LayoutParams;
    import android.app.Activity;
    import android.content.Context;
    import android.graphics.drawable.BitmapDrawable;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.PopupWindow;
    
    public class TestPopwindow2 extends PopupWindow {
    	// 根视图
    	private View mRootView;
    	// LayoutInflater
    	LayoutInflater mInflater;
    
    	public TestPopwindow2(Activity context) {
    		super(context);
    		mInflater = (LayoutInflater) context
    				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    		mRootView = mInflater.inflate(R.layout.test_popwindow_2, null);
    		setContentView(mRootView);
    
    		this.setWidth(LayoutParams.WRAP_CONTENT);
    		this.setHeight(LayoutParams.WRAP_CONTENT);
    
    		// 设置PopUpWindow弹出的相关属性
    		setTouchable(true);
    		setOutsideTouchable(true);
    		setFocusable(true);
    		setBackgroundDrawable(new BitmapDrawable(context.getResources()));
    		update();
    
    		getContentView().setFocusableInTouchMode(true);
    		getContentView().setFocusable(true);
    		setAnimationStyle(R.style.AppBaseTheme);
    	}
    }
    
    MainActivity.class

    package com.example.popwindowdemo;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Gravity;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.PopupWindow.OnDismissListener;
    import android.widget.Toast;
    
    public class MainActivity extends Activity implements OnClickListener,
    		OnDismissListener {
    
    	private TestPopwindow2 mTestPopwindow2 = null;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    
    		InitUI();
    
    	}
    
    	private void InitUI() {
    		// 实例化TestPopwindow2
    		mTestPopwindow2 = new TestPopwindow2(this);
    		// 设置点击其它位置mTestPopwindow2消失
    		mTestPopwindow2.setOnDismissListener(this);
    
    		Button buttonTest2 = (Button) findViewById(R.id.buttonTest2);
    		buttonTest2.setOnClickListener(this);
    	}
    
    	private void OnPopwindowTest2() {
    		if (mTestPopwindow2 == null)
    			return;
    
    		// location获得控件的位置
    		int[] location = new int[2];
    		View v = findViewById(R.id.buttonTest2);
    		if (v != null)
    			v.getLocationOnScreen(location);   // 控件在屏幕的位置
    		mTestPopwindow2.setAnimationStyle(R.style.AppBaseTheme);
    		
    		// mTestPopwindow2弹出在某控件(button)的以下
    		mTestPopwindow2.showAtLocation(v, Gravity.TOP | Gravity.LEFT,
    				location[0] - v.getWidth(), location[1] + v.getHeight());
    	}
    
    	// mTestPopwindow2布局控件的监听
    	public void OnclickTestListener(View view) {
    		switch (view.getId()) {
    		case R.id.layoutSeclect1:
    			Toast.makeText(this, "系统热门方案", Toast.LENGTH_SHORT).show();
    			break;
    		case R.id.layoutSeclect2:
    			Toast.makeText(this, "个人热门方案", Toast.LENGTH_SHORT).show();
    			break;
    		default:
    			break;
    		}
    		if (mTestPopwindow2 != null)
    			mTestPopwindow2.dismiss();
    	}
    
    	@Override
    	public void onClick(View v) {
    		switch (v.getId()) {
    		case R.id.buttonTest2:
    			OnPopwindowTest2();
    			break;
    		default:
    			break;
    		}
    	}
    
    	// 点击其它地方消失
    	@Override
    	public void onDismiss() {
    	}
    }
    
    test_popwindow_2.xml
    <?

    xml version="1.0" encoding="UTF-8"?

    > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#0000" > <!-- popupwimdow的布局注意不能使用相似 android:layout_marginTop=""这样属性,特别是相对布局时候. 布局或者控件总体靠左等都会影响popupwimdow定位使用 --> <LinearLayout android:id="@+id/hot_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/title_function_bg" android:orientation="vertical" > <LinearLayout android:id="@+id/layoutSeclect1" android:layout_width="match_parent" android:layout_height="wrap_content" android:clickable="true" android:onClick="OnclickTestListener" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp" android:text="系统热门方案" android:textColor="#fff" android:textSize="18sp" /> </LinearLayout> <LinearLayout android:id="@+id/layoutSeclect2" android:layout_width="match_parent" android:layout_height="wrap_content" android:clickable="true" android:onClick="OnclickTestListener" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp" android:text="个人热门方案" android:textColor="#fff" android:textSize="18sp" /> </LinearLayout> </LinearLayout> </LinearLayout>

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/LinearLayoutMain"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
    
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right">
            
        <Button
            android:id="@+id/buttonTest2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="測试2" />
        </LinearLayout>
    
    </LinearLayout>
    
    效果图例如以下

    项目资源下载:点击这里

    转载请注明出处的博客网址: http://blog.csdn.net/qq_16064871
    如有转载未注明出处博客网址,或者没有得到作者的允许。

    作者持有版权全部权。



  • 相关阅读:
    linux笔记2.20
    新安装ubuntu后几项配置
    linux网络配置
    linux笔记2.19
    jquery在不同浏览器获取文件路径出现问题!
    软件工程师如何定义自己的职业路线
    标识符
    结对项目-四则运算 “软件”之升级版
    小学四则运算 “软件”之初版
    分布式版本控制系统Git的安装与使用
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/6724830.html
Copyright © 2020-2023  润新知