<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 2.6.1 PopupWindow(懸浮框)的基本使用 ## 本節引言: > 本節給大家帶來的是最后一個用于顯示信息的UI控件——PopupWindow(懸浮框),如果你想知道 他長什么樣子,你可以打開你手機的QQ,長按列表中的某項,這個時候后彈出一個黑色的小 對話框,這種就是PopupWindow了,和AlertDialog對話框不同的是,他的位置可以是隨意的; > > 另外AlertDialog是非堵塞線程的,而PopupWindow則是堵塞線程的!而官方有這樣一句話來介紹 PopupWindow: > > **A popup window that can be used to display an arbitrary view. The popup window is** > > **a floating container that appears on top of the current activity.** > > 大概意思是:一個彈出窗口控件,可以用來顯示任意View,而且會浮動在當前activity的頂部 > > 下面我們就來對這個控件進行學習~ > > 官方文檔:[PopupWindow](http://androiddoc.qiniudn.com/reference/android/widget/PopupWindow.html) ## 1.相關方法解讀 ### 1)幾個常用的構造方法 > 我們在文檔中可以看到,提供給我們的PopupWindow的構造方法有九種之多,這里只貼實際 開發中用得較多的幾個構造方法: > > * **public PopupWindow (Context context)** > * **public PopupWindow(View contentView, int width, int height)** > * **public PopupWindow(View contentView)** > * **public PopupWindow(View contentView, int width, int height, boolean focusable)** > > 參數就不用多解釋了吧,contentView是PopupWindow顯示的View,focusable是否顯示焦點 ### 2)常用的一些方法 > 下面介紹幾個用得較多的一些方法,其他的可自行查閱文檔: > > * **setContentView**(View contentView):設置PopupWindow顯示的View > * **getContentView**():獲得PopupWindow顯示的View > * **showAsDropDown(View anchor)**:相對某個控件的位置(正左下方),無偏移 > * **showAsDropDown(View anchor, int xoff, int yoff)**:相對某個控件的位置,有偏移 > * **showAtLocation(View parent, int gravity, int x, int y)**: 相對于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以設置偏移或無偏移 PS:parent這個參數只要是activity中的view就可以了! > * **setWidth/setHeight**:設置寬高,也可以在構造方法那里指定好寬高, 除了可以寫具體的值,還可以用WRAP_CONTENT或MATCH_PARENT, popupWindow的width和height屬性直接和第一層View相對應。 > * **setFocusable(true)**:設置焦點,PopupWindow彈出后,所有的觸屏和物理按鍵都由PopupWindows 處理。其他任何事件的響應都必須發生在PopupWindow消失之后,(home 等系統層面的事件除外)。 比如這樣一個PopupWindow出現的時候,按back鍵首先是讓PopupWindow消失,第二次按才是退出 activity,準確的說是想退出activity你得首先讓PopupWindow消失,因為不并是任何情況下按back PopupWindow都會消失,必須在PopupWindow設置了背景的情況下 。 > * **setAnimationStyle(int):**設置動畫效果 ## 2.使用代碼示例 **運行效果圖**: ![](http://www.runoob.com/wp-content/uploads/2015/10/43925301.jpg) **實現關鍵代碼**: 先貼下動畫文件:**anim_pop.xml**: ``` <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="0" android:toAlpha="1" android:duration="2000"> </alpha> </set> ``` 接著是popupWindow的布局:**item_popip.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:background="@drawable/ic_pop_bg" android:orientation="vertical"> <Button android:id="@+id/btn_xixi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" android:text="嘻嘻" android:textSize="18sp" /> <Button android:id="@+id/btn_hehe" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" android:text="呵呵" android:textSize="18sp" /> </LinearLayout> ``` **MainActivity.java**: ``` public class MainActivity extends AppCompatActivity { private Button btn_show; private Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = MainActivity.this; btn_show = (Button) findViewById(R.id.btn_show); btn_show.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { initPopWindow(v); } }); } private void initPopWindow(View v) { View view = LayoutInflater.from(mContext).inflate(R.layout.item_popup, null, false); Button btn_xixi = (Button) view.findViewById(R.id.btn_xixi); Button btn_hehe = (Button) view.findViewById(R.id.btn_hehe); //1.構造一個PopupWindow,參數依次是加載的View,寬高 final PopupWindow popWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true); popWindow.setAnimationStyle(R.anim.anim_pop); //設置加載動畫 //這些為了點擊非PopupWindow區域,PopupWindow會消失的,如果沒有下面的 //代碼的話,你會發現,當你把PopupWindow顯示出來了,無論你按多少次后退鍵 //PopupWindow并不會關閉,而且退不出程序,加上下述代碼可以解決這個問題 popWindow.setTouchable(true); popWindow.setTouchInterceptor(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return false; // 這里如果返回true的話,touch事件將被攔截 // 攔截后 PopupWindow的onTouchEvent不被調用,這樣點擊外部區域無法dismiss } }); popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000)); //要為popWindow設置一個背景才有效 //設置popupWindow顯示的位置,參數依次是參照View,x軸的偏移量,y軸的偏移量 popWindow.showAsDropDown(v, 50, 0); //設置popupWindow里的按鈕的事件 btn_xixi.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "你點擊了嘻嘻~", Toast.LENGTH_SHORT).show(); } }); btn_hehe.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "你點擊了呵呵~", Toast.LENGTH_SHORT).show(); popWindow.dismiss(); } }); } } ``` ## 3.示例代碼下載 [PopWindowDemo.zip](http://www.runoob.com/wp-content/uploads/2015/10/PopWindowDemo.zip) ## 本節小結: > 時間關系,并沒有想到好一點的示例,就寫了一個簡單的demo,應該能滿足簡單的需要,另外 如果想深入研究下PopupWindow的話可看下述的參考文獻: > [Android PopupWindow的使用和分析](http://www.cnblogs.com/mengdd/p/3569127.html) > [Android PopupWindow詳解](http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0702/1627.html) > 嗯,就說這么多,謝謝~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看