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

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

PopupWindow使用方法詳解

2019-12-12 02:08:26
字體:
供稿:網(wǎng)友

學習了Android PopupWindow的使用技巧 【Android UI設計與開發(fā)】7.底部菜單欄(四)PopupWindow 實現(xiàn)顯示仿騰訊新聞底部彈出菜單,然后自己進行了一下研究,寫一個總結(jié),方便以后學習。

效果圖:


1.PopupWindow的布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:background="@color/colorAccent"  android:gravity="center"  android:layout_width="match_parent"  android:layout_height="wrap_content">  <TextView    android:id="@+id/tv_popup_text"    android:layout_width="wrap_content"    android:layout_height="80dp"    android:text="我就是彈窗"    android:textSize="25sp"    android:textColor="#ffffffff"    android:layout_centerInParent="true"    android:gravity="center"/></LinearLayout>

2.在res下新建anim文件夾,為窗口彈出消失寫動畫:

popupwindow_in:

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">  <translate    android:duration="250"    android:fromYDelta="100.0%"    android:toYDelta="0.0" /></set>

popupwindow_out:

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">  <translate    android:duration="250"    android:fromYDelta="0.0"    android:toYDelta="100%" /></set>

添加style:

 <style name="anim_popup_window">    <item name="android:windowEnterAnimation">@anim/popupwindow_in</item>    <item name="android:windowExitAnimation">@anim/popupwindow_out</item>  </style>

3.主界面布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:app="http://schemas.android.com/apk/res-auto"  xmlns:tools="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:gravity="center"  android:orientation="vertical"  android:id="@+id/layout_home"  android:background="#FFB5C5"  tools:context="com.lotus.popupwindowdemo.HomeActivity">  <TextView    android:id="@+id/tv_show_popup_window"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:textSize="20sp"    android:text="點擊顯示PopupWindow" /></LinearLayout>

4.主界面代碼:

import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.widget.LinearLayout;import android.widget.LinearLayout.LayoutParams;import android.widget.PopupWindow;import android.widget.TextView;import android.widget.Toast;public class HomeActivity extends AppCompatActivity implements View.OnClickListener {  private LinearLayout layout_home;  private TextView tv_show_popup_window;  private PopupWindow mPopupWindow;  private TextView tv_popup_text;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_home);    // 引入窗口配置文件:即彈窗的界面    View popupView = getLayoutInflater().inflate( R.layout.layout_popupwindow, null);    popupView.setOnClickListener( this);    tv_popup_text = (TextView) popupView.findViewById(R.id.tv_popup_text);    tv_popup_text.setOnClickListener(this);    // PopupWindow實例化    mPopupWindow = new PopupWindow( popupView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, true);    // 設置PopupWindow是否可觸摸(設置為不可觸摸,那彈出框內(nèi)的任何控件都不能進行任何點擊等等類似操作)    mPopupWindow.setTouchable( true);    // 設置非PopupWindow區(qū)域是否可觸摸    // 1.若設置PopupWindow獲得焦點和非PopupWindow區(qū)域可觸摸,但實際上非PopupWindow區(qū)域的控件并不能響應點擊事件等等    // 2.若設置PopupWindow不可獲得焦點,則不管非PopupWindow區(qū)域被設置能否觸摸,實際上非PopupWindow區(qū)域的控件都能響應點擊事件等等    // 3.若設置PopupWindow不可獲得焦點,非PopupWindow區(qū)域被設置能觸摸,當點擊非PopupWindow區(qū)域時能隱藏PopupWindow,而點擊返回鍵并不能隱藏窗口,    //  此時通過按鈕只能控制窗口的彈出,并不能控制消失,消失只能通過點擊其他非PopupWindow區(qū)域    mPopupWindow.setOutsideTouchable( false);    // 如果不設置PopupWindow的背景,無論是點擊外部區(qū)域還是Back鍵都無法dismiss彈框(但目前并沒有發(fā)現(xiàn)此問題)//    mPopupWindow.setBackgroundDrawable( new BitmapDrawable( getResources(), (Bitmap) null));    // 設置PopupWindow顯示和隱藏時的動畫    mPopupWindow.setAnimationStyle(R.style.anim_popup_window);    // 設置PopupWindow是否可獲得焦點    // 1.如果設置為可獲得焦點,不管非PopupWindow區(qū)域被設置能否觸摸,也會在點擊屏幕非PopupWindow區(qū)域和點擊返回鍵時,使PopupWindow隱藏    // 2.相反,如果設置為不可獲得焦點,在點擊屏幕非PopupWindow區(qū)域或點擊返回鍵時,都不能使PopupWindow隱藏    mPopupWindow.setFocusable(false);    layout_home = (LinearLayout) this.findViewById(R.id.layout_home);    tv_show_popup_window = (TextView) this.findViewById( R.id.tv_show_popup_window);    tv_show_popup_window.setOnClickListener(new View.OnClickListener() {      @Override      public void onClick(View view) {        if ( mPopupWindow.isShowing()) {          // 隱藏窗口,如果設置了點擊窗口外消失,則不需要此方式隱藏          mPopupWindow.dismiss();          tv_show_popup_window.setText("點擊顯示PopupWindow");        } else {          // 彈出窗口顯示內(nèi)容視圖,默認以錨定視圖的左下角為起點,這里為點擊按鈕//        mPopupWindow.showAsDropDown( view);//默認在view(tv_show_popup_window)的下方出現(xiàn)          mPopupWindow.showAtLocation( layout_home, Gravity.BOTTOM, 0, 0);          tv_show_popup_window.setText("點擊使PopupWindow消失");        }      }    });  }  @Override  public void onClick(View view) {    switch ( view.getId()){      case R.id.tv_popup_text:        Toast.makeText( getApplicationContext(),"我是PopupWindow內(nèi)的一個控件",Toast.LENGTH_SHORT).show();        break;    }  }}

注:分析屬性時,注釋寫得有點多,因為發(fā)現(xiàn)屬性彼此間聯(lián)系緊密,所以要小心使用才行。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 兴化市| 巴南区| 九龙坡区| 凌源市| 深泽县| 青海省| 玉田县| 当涂县| 康平县| 浏阳市| 江都市| 广西| 博乐市| 龙泉市| 海宁市| 睢宁县| SHOW| 盱眙县| 新绛县| 永川市| 柳州市| 武冈市| 工布江达县| 久治县| 遵义县| 万安县| 香港 | 玉环县| 邹城市| 昌宁县| 黔西县| 文山县| 北流市| 乐山市| 会同县| 新余市| 伊宁市| 英山县| 宁国市| 灵台县| 亳州市|