PopupWindow 사용 방법 상세 설명
6886 단어 PopupWindow쓰다
효과 그림:
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;
}
}
}
주:속성 을 분석 할 때 주석 을 좀 많이 썼 습 니 다.속성 이 서로 밀접 하 게 연결 되 어 있 는 것 을 발 견 했 기 때문에 조심스럽게 사용 해 야 합 니 다.이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
페이지에 한마디 쓰기helloworld! innerHTML 메서드 appendChild(),createTextNode() 메서드 쓰기 좀 더 복잡한 This is mycontent....
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.