手把手教popupWindow从下往上,以达到流行效果

时间:2023-03-09 12:53:22
手把手教popupWindow从下往上,以达到流行效果

效果如图所看到的,点击開始button,popWindow从下往上出来,再点击popWindow外面,popWindow又从上往下消失

手把手教popupWindow从下往上,以达到流行效果

能够看出来,上面的popupWindow是半透明的,后面我会细说。

最主要的是activity_main了,非常easy,就仅仅是一个button,这里我就不贴代码了。

接下来的是,popWindow的界面了

手把手教popupWindow从下往上,以达到流行效果

代码例如以下:这里注意我里面的那个凝视

<?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="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical" > <!-- 这里的linearLayout加android:background=""这个属性要慎重,假设加了后,popwindow是不能半透明了的 --> <Button
android:id="@+id/first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="@android:color/holo_red_light"
android:text="第一个button" /> <Button
android:id="@+id/second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@android:color/holo_red_light"
android:text="第二个button" /> <Button
android:id="@+id/third"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@android:color/holo_red_light"
android:text="第三个button" /> </LinearLayout>

然后在res/下新建一个目录anim,进而anim下新建两个xml文件,如图所看到的:

手把手教popupWindow从下往上,以达到流行效果

当中,pophidden_anim的代码例如以下

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"> <translate
android:duration="1000"
android:fromYDelta="0"
android:toYDelta="50%p" /> <alpha
android:duration="1000"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>

popshow_anim的代码例如以下

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

然后在values/styles.xml增加下面代码,变成这个样子,上面的那些是自带的

<resources>

    <!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices. -->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here. -->
</style> <!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style> <!-- 这个是增加的代码 -->
<style name="mypopwindow_anim_style">
<item name="android:windowEnterAnimation">@anim/popshow_anim</item>
<!-- 指定显示的动画xml --> <item name="android:windowExitAnimation">@anim/pophidden_anim</item>
<!-- 指定消失的动画xml -->
</style> </resources>

之后就是Activity里面的代码了,我里面都写好了全部的凝视,应该能够看得非常清楚

package com.example.popwindow;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.Toast; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start = (Button) findViewById(R.id.start);
start.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
showPopwindow();
} });
} /**
* 显示popupWindow
*/
private void showPopwindow() {
// 利用layoutInflater获得View
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.popwindowlayout, null); // 以下是两种方法得到宽度和高度 getWindow().getDecorView().getWidth() PopupWindow window = new PopupWindow(view,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT); // 设置popWindow弹出窗口可点击,这句话必须加入,而且是true
window.setFocusable(true); // 实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0xb0000000);
window.setBackgroundDrawable(dw); // 设置popWindow的显示和消失动画
window.setAnimationStyle(R.style.mypopwindow_anim_style);
// 在底部显示
window.showAtLocation(MainActivity.this.findViewById(R.id.start),
Gravity.BOTTOM, 0, 0); // 这里检验popWindow里的button能否够点击
Button first = (Button) view.findViewById(R.id.first);
first.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) { System.out.println("第一个按钮被点击了");
}
}); //popWindow消失监听方法
window.setOnDismissListener(new OnDismissListener() { @Override
public void onDismiss() {
System.out.println("popWindow消失");
}
}); }
}

当中window.setFocusable(true)和window.setBackgroundDrawable()必须填写,假设是想让popWindow半透明,就是上面的那个方法,

假设仅仅是单纯的调用这种方法就这样写window.setBackgroundDrawable(new BitmapDrawable());

关于popupWindow显示位置的详细方法,你能够看这个博客popupWindow在指定位置上的显示

这个资源我已经上传,0积分下载,有须要的能够下载看一看点击这里下载