AndroidのPopupWindowは、WeChatの右上隅にあるポップアップメニューに似ています。

毎日の開発プロセスでのPopupWindownの使用も比較的大きいです。ここでは、PopupWindowの使用方法を示します。

activity_main.xmlレイアウトを変更します。

<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:tools = "http://schemas.android.com/tools"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = "$ {relativePackage}。$ {activityClass}">

<RelativeLayout
android:layout_width = "match_parent"
android:layout_height = "50dip"
android:background = "@ android:color / holo_blue_dark">

< ImageView
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_centerVertical = "true"
android:layout_marginLeft = "10dip"
android:background = "@ drawable / ic_launcher" />

<ImageView
android:id = "@ + id / rl_more"
android:layout_width = "wrap_content"
android:layout_height = "match_parent"
android:background = "@ drawable / ability_show_item_bg"
android:paddingLeft = "15dp"
android:paddingRight = "5dp"
android:layout_alignParentRight = "true"
android:src = " @ drawable / actionbar_more_icon "/>

</ RelativeLayout>

</ RelativeLayout>

新建popup_window.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 = "@ android:color / white"
android:gravity = "center_horizo​​ntal"
android:orientation = "vertical">

<TextView
android:id = "@ + id / settings"
android:layout_width = "match_parent"
android:layout_height = "45dp"
android:gravity = "center"
android:padding = "12dp"
android:text = "设置"
android:textSize = "16sp" />

<
android:layout_width = "match_parent"
android:layout_height = "1dp"を表示
android:background = "#BDBDBD" />

<TextView
android:id = "@ + id / about"
android:layout_width = "match_parent"
android:layout_height = "45dp"
android:gravity = "center"
android:padding = "12dp "
android:text ="关于 "
android:textSize =" 16sp "/>

<View
android:layout_width =" match_parent "
android:layout_height =" 1dp "
android:background ="#BDBDBD "/>

<TextView
android:id =" @ + id / ability_logout "
android:layout_width =" match_parent "
android:layout_height =" 45dp "
android:gravity =" center "
android:padding = "12dp"
android:text = "退出"
android:textSize = "16sp" />

</ LinearLayout>

自定义PopupWindow类PopWindow
パッケージcom.syz.mypopupwindow;

android.app.Activityをインポートします。
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;

/ **
* <p> Title:PopWindow </ p>
* <p>説明:自定义PopupWindow </ p>
* @author syz
* @date 2016-3-14
* /
public class PopWindow extends PopupWindow {
private View conentView ;
public PopWindow(final Activity context){
LayoutInflater inflater =(LayoutInflater)context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
conentView = inflater.inflate(R.layout.popup_window、null);
int h = context.getWindowManager()。getDefaultDisplay()。getHeight();
int w = context.getWindowManager()。getDefaultDisplay ().getWidth();
//設定SelectPicPopupWindow表示
this.setContentView(conentView);
//ポップアップ形式を設定SelectPicPopupWindow幅
this.setWidth(W + 40/2);
//設定ポップアップ形式の高いSelectPicPopupWindow
この.setHeight(LayoutParams.WRAP_CONTENT);
// SelectPicPopupWindowポップアップウィンドウをクリックしてthis.setFocusable
(true);
this.setOutsideTouchable(true);
//状態を更新
this.update();
//ハーフカラーでColorDrawableをインスタンス化する透明な
ColorDrawable dw =新しいColorDrawable(0000000000);
//ポイントバックキーと他の場所では、それが消えるよう、OnDismisslistenerをトリガするために、これを設定し、設定された他のコントロール変更操作
this.setBackgroundDrawable(DW);
// mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
//セットSelectPicPopupWindowポップアップ形式のアニメーション
this.setAnimationStyle(R.style.AnimationPreview);

新新(){にconentView.findViewById(R.id.about).setOnClickListener(OnClickListener

@Override
無効公共のonClick(表示arg0が){
//もし何かをここでは壁紙の必要性
PopWindow.this.dismiss();
}
});
conentView.findViewById(R.id.ability_logout).setOnClickListener(新新にOnClickListener(){

@Override
ます。public void onClickの(ビューのarg0){
//がOUT署名する前に何かをする
コンテキストを.finish();
PopWindow.this.dismiss();
}
});
conentView.findViewById(R.id.settings).setOnClickListener(新しいOnClickListener(){

@Override
ます。public void onClickの(ビューのarg0){
//ここで必要な何か

PopWindow.this.dismissを();
}
});
}

/ **
*显poppopWindow
*
* @param parent
* /
public void showPopupWindow(View parent){
if(!this.isShowing()){
//以下拉方式显示popupwindow
this.showAsDropDown(parent、parent.getLayoutParams() .width / 2、5);
} else {
this.dismiss();
}
}
}

カスタムPopupWindowスタイル、
AnimationPreviewを追加する

<style name = "AnimationPreview">
<item name = "android:windowEnterAnimation"> @ anim / fade_in </ item>
<item name = "android:windowExitAnimation"> @ anim / fade_out </ item>
</ style>

スタイル
fade_in.xmlに必要なアニメーションを追加

<?xml version = "1.0" encoding = "utf-8"?>
<!-左上角扩大->
<scale xmlns:android = "http://schemas.android.com/apk/res/android"
android:interpolator = "@ android:anim / accelerate_decelerate_interpolator"
android:fromXScale = "0.001"
android:toXScale = "1.0"
android:fromYScale = "0.001"
android:toYScale = "1.0"
android:pivotX = "100%"
android: pivotY = "10%"
android:duration = "200" />

fade_out.xml
<?xml version = "1.0" encoding = "utf-8"?>
<!-左上角缩小->
<scale xmlns:android = "http://schemas.android.com/apk/res / android "
android:interpolator =" @ android:anim / accelerate_decelerate_interpolator "
android:fromXScale =" 1.0 "
android:toXScale =" 0.001 "
android:fromYScale =" 1.0 "
android:toYScale =" 0.001 "
android:pivotX =" 100% "
android:pivotY =" 10% "
android:duration =" 200 "/>


最後にMainActivityで
パッケージcom.syz.mypopupwindowを使用します。

android.app.Activityをインポートします。
android.os.Bundleをインポートします。
import android.view.View;
import android.view.View.OnClickListener;

パブリッククラスMainActivityはActivityがOnClickListenerを拡張します{

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.rl_more).setOnClickListener(this);
}

@Override
public void onClick(View v){
if(v.getId()== R.id.rl_more){
PopWindow popWindow = new PopWindow(this);
popWindow.showPopupWindow(findViewById(R.id.rl_more));
}
}
}

効果画像:
    

————————————————
著作権に関する声明:この記事はCSDNブロガー「Elven_Shi」によるオリジナルの記事であり、CC 4.0 BY-SAの著作権契約に従います。元のソースリンクとこの声明を添付して転載してください。
元のリンク:https://blog.csdn.net/loveyaozu/java/article/details/51150229

おすすめ

転載: www.cnblogs.com/H-xiaofeng/p/12729370.html