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;// LayoutInflaterLayoutInflater 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;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);InitUI();}private void InitUI() {// 实例化TestPopwindow2mTestPopwindow2 = 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();}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.buttonTest2:OnPopwindowTest2();break;default:break;}}// 点击其他地方消失@Overridepublic void onDismiss() {}}test_popwindow_2.xml<?xml version="1.0" encoding="UTF-8"?><LinearLayout xmlns:android=""android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="#0000" ><!– popupwimdow的布局注意不能使用类似android:layout_marginTop=""这样属性,特别是相对布局时候. 布局或者控件整体靠左等都会影响popupwimdow定位使用–><LinearLayoutandroid:id="@+id/hot_layout"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/title_function_bg"android:orientation="vertical" ><LinearLayoutandroid:id="@+id/layoutSeclect1"android:layout_width="match_parent"android:layout_height="wrap_content"android:clickable="true"android:onClick="OnclickTestListener" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="8dp"android:text="系统热门方案"android:textColor="#fff"android:textSize="18sp" /></LinearLayout><LinearLayoutandroid:id="@+id/layoutSeclect2"android:layout_width="match_parent"android:layout_height="wrap_content"android:clickable="true"android:onClick="OnclickTestListener" ><TextViewandroid: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

忘掉失败,不过要牢记失败中的教训。

Android popupwindow 示例程序一

相关文章:

你感兴趣的文章:

标签云: