国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Popupwindow 的簡單實用案例(顯示在控件下方)

2019-12-12 03:05:42
字體:
供稿:網(wǎng)友

第一步:

private PopupWindow mPopupWindow;

第二步:寫一個popupwindow的布局文件XML

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical">  <RelativeLayout    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#669E9E9E"><LinearLayout  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:orientation="horizontal"  android:background="#E4E4E4"  >  <TextView    android:id="@+id/popupwindow_Jan"    android:layout_width="0dp"    android:layout_height="wrap_content"    android:layout_weight="1"    android:text="一月份"    android:gravity="center"    />  <TextView    android:id="@+id/popupwindow_Feb"    android:layout_width="0dp"    android:layout_height="wrap_content"    android:layout_weight="1"    android:text="二月份"    android:gravity="center"    />  <TextView    android:id="@+id/popupwindow_Mar"    android:layout_width="0dp"    android:layout_height="wrap_content"    android:layout_weight="1"    android:text="三月份"    android:gravity="center"    /></LinearLayout>  </RelativeLayout></LinearLayout>

第三步:在Activity寫代碼

public void onClick(View v) {  switch (v.getId()) {   case R.id.home_travel_modes_yuefen_textview:       showPopupWindow(v);      break;   case R.id.popupwindow_Jan:      showToastMsg("一月份");      break;    case R.id.popupwindow_Feb:      showToastMsg("二月份");      break;    default:      break;  } public void showPopupWindow(View v){    View contentView = LayoutInflater.from(HomeTravelModesActivity.this).inflate(R.layout.home_popuplayout, null);    TextView JanText = (TextView)contentView.findViewById(R.id.popupwindow_Jan);    TextView FebText = (TextView)contentView.findViewById(R.id.popupwindow_Feb);    TextView MarText = (TextView)contentView.findViewById(R.id.popupwindow_Mar);    JanText.setOnClickListener(this);    FebText.setOnClickListener(this);    MarText.setOnClickListener(this);    final PopupWindow popupWindow = new PopupWindow(contentView,        LinearLayout.LayoutParams.MATCH_PARENT, 300, true);    popupWindow.setTouchable(true);//    popupWindow.setTouchInterceptor(new View.OnTouchListener() {////      @Override//      public boolean onTouch(View v, MotionEvent event) {////        Log.i("mengdd", "onTouch : ");////        return false;//        // 這里如果返回true的話,touch事件將被攔截//        // 攔截后 PopupWindow的onTouchEvent不被調(diào)用,這樣點擊外部區(qū)域無法dismiss//      }//    });    // 如果不設(shè)置PopupWindow的背景,無論是點擊外部區(qū)域還是Back鍵都無法dismiss彈框    // 我覺得這里是API的一個bug    popupWindow.setBackgroundDrawable(getResources().getDrawable(        R.mipmap.ic_launcher));    // 設(shè)置好參數(shù)之后再show    popupWindow.showAsDropDown(v);  }

注:

若在Activity的onCreate()方法中直接寫彈出PopupWindow()方法報錯,因為Activity沒有完全啟動是不能彈出PopupWindow的,那我們只需要在Activity完全啟動后在彈出PopupWindow就行了。

重寫一下onWindowFocusChanged()方法:

@Overridepublic void onWindowFocusChanged(boolean hasFocus) {  super.onWindowFocusChanged(hasFocus);  //彈出PopupWindow的具體代碼}

以上這篇Popupwindow 的簡單實用案例(顯示在控件下方)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 漯河市| 剑河县| 蓝山县| 双牌县| 祁阳县| 法库县| 古浪县| 三门县| 昌宁县| 都江堰市| 平阳县| 宝丰县| 芦山县| 永善县| 项城市| 鄄城县| 乐安县| 抚松县| 澄迈县| 翼城县| 乐清市| 内丘县| 叙永县| 兴安盟| 弥勒县| 高雄县| 南昌市| 黎平县| 江安县| 黑山县| 灵武市| 江源县| 昌吉市| 河北区| 曲麻莱县| 济宁市| 涟水县| 涞水县| 夏邑县| 汶川县| 阿克|