PopupWindow 사용 방법 상세 설명

6886 단어 PopupWindow쓰다
4.567915.와 4.567915.를 공부 한 후에 스스로 연 구 를 실시 하여 총 결 을 써 서 나중에 공부 하기에 편리 하 다.
효과 그림:

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 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     (       ,                         )
    mPopupWindow.setTouchable( true);
    //    PopupWindow       
    // 1.   PopupWindow      PopupWindow     ,     PopupWindow                
    // 2.   PopupWindow      ,    PopupWindow         ,    PopupWindow               
    // 3.   PopupWindow      , PopupWindow        ,    PopupWindow      PopupWindow,             ,
    //                 ,       ,           PopupWindow  
    mPopupWindow.setOutsideTouchable( false);
    //      PopupWindow   ,           Back    dismiss  (           )
//    mPopupWindow.setBackgroundDrawable( new BitmapDrawable( getResources(), (Bitmap) null));
    //   PopupWindow         
    mPopupWindow.setAnimationStyle(R.style.anim_popup_window);
    //   PopupWindow       
    // 1.          ,   PopupWindow         ,        PopupWindow         , PopupWindow  
    // 2.  ,           ,      PopupWindow         ,    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 {
          //           ,              ,       
//        mPopupWindow.showAsDropDown( view);//   view(tv_show_popup_window)     
          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      ",Toast.LENGTH_SHORT).show();
        break;
    }
  }
}
주:속성 을 분석 할 때 주석 을 좀 많이 썼 습 니 다.속성 이 서로 밀접 하 게 연결 되 어 있 는 것 을 발 견 했 기 때문에 조심스럽게 사용 해 야 합 니 다.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기