popupWindow的使用心得

來源:互聯網
上載者:User

首先,要為popupWindow 寫一個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="wrap_content"    >    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="@dimen/margin_max"        android:layout_marginRight="@dimen/margin_max"        android:background="@color/white"        android:orientation="vertical" >        <!-- main content -->         <TextView            android:id="@+id/diet_pop_tv1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="@dimen/margin_max"            android:text="@string/activity_aerobic"            android:textColor="@color/string_bgwhite_main"            android:textSize="@dimen/text_15" />         <!-- main content sub -->          <TextView            android:id="@+id/diet_pop_tv2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/recipe_choose_exam"            android:layout_marginLeft="@dimen/margin_max"            android:textColor="@color/string_bgwhite_sub"            android:textSize="@dimen/text_15" />        <!-- second title -->          <TextView            android:id="@+id/diet_pop_tv3"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:background="@drawable/diet_pop_title"            android:paddingLeft="@dimen/margin_max"            android:layout_marginLeft="@dimen/margin_max"            android:text="@string/activity_aerobic_content"            android:textColor="@color/white"            android:textSize="@dimen/text_15"            />        <!-- second content -->          <TextView            android:id="@+id/diet_pop_tv4"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="@dimen/margin_max"            android:text="@string/activity_aerobic_suggest"            android:textColor="@color/string_bgwhite_main"            android:textSize="@dimen/text_15" />        <ImageViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:scaleType="fitStart"android:background="@drawable/popupshadow"            />    </LinearLayout>  </LinearLayout>---------然後---再代碼中處理popupWindow, // new popupwindowView inflateView = getLayoutInflater().inflate(R.layout.recipe_popwindow, null);//上面的xml檔案,作為popupWindow的視圖mPopupWindow = new PopupWindow(inflateView, LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);mPopupWindow.setFocusable(false);mPopupWindow.setOutsideTouchable(true);mPopupWindow.setAnimationStyle(R.style.AnimationPreview);//為popupWindow設定進入,退出的動畫效果------下面配置進入退出的動畫: anim檔案夾下,進入動畫(由右下角進入,由小變大): <?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"        android:interpolator="@android:anim/decelerate_interpolator">    <scale android:fromXScale=".1" android:toXScale="1.0"           android:fromYScale=".1" android:toYScale="1.0"           android:pivotX="100%p" android:pivotY="100%p"            android:duration="500"/>    <alpha android:fromAlpha="0" android:toAlpha="1.0"              android:duration="@android:integer/config_mediumAnimTime"/></set>退齣動畫(向右下角退出,由大變小,變透明): <?xml version="1.0" encoding="utf-8"?>  <set xmlns:android="http://schemas.android.com/apk/res/android"          android:interpolator="@android:anim/decelerate_interpolator"          android:zAdjustment="top">      <scale android:fromXScale="1.0" android:toXScale=".5"             android:fromYScale="1.0" android:toYScale=".5"             android:pivotX="100%p" android:pivotY="100%p"             android:duration="@android:integer/config_mediumAnimTime" />      <alpha android:fromAlpha="1.0" android:toAlpha="0"              android:duration="@android:integer/config_mediumAnimTime"/>  </set> --------在styles檔案夾下配置style: <style name="AnimationPreview"><item name="android:windowEnterAnimation">@anim/zoomin</item><item name="android:windowExitAnimation">@anim/zoomout</item></style>-------最後在代碼中設定popupWindow進入退出的判定: public void openMenu(View v) {// System.out.println(view.getId() + "-----id--" + view.getBottom()// + "bottom-----view" + view.getTop());if (!flag) {//flag初始化為false;// set position// mPopupWindow.showAtLocation(view, Gravity.LEFT, 0, 0);mPopupWindow.showAsDropDown(v, 0, 0);// view.findViewById(R.id.diet_sugget_ka).setVisibility(View.INVISIBLE);flag = true;} else {mPopupWindow.dismiss();flag = false;}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.