標籤: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="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
如有轉載未註明出處部落格網址,或者沒有得到作者的同意。作者持有著作權權。
Android popupwindow 樣本程式一