PopupWindowActivity以及页面布局文件
package com.example.test0508;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.example.test0508.util.ToastUtil;
public class PopupWindowActivity extends AppCompatActivity {
private Button mBtnPop;
private PopupWindow mPopWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popup_window);
mBtnPop = findViewById(R.id.btn_pop);
mBtnPop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View view = getLayoutInflater().inflate(R.layout.layout_pop,null);
TextView textView = view.findViewById(R.id.tv_pop_good);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mPopWindow.dismiss();
ToastUtil.showMessage(PopupWindowActivity.this,"点击了好");
}
});
mPopWindow = new PopupWindow(view,mBtnPop.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);
mPopWindow.setOutsideTouchable(true);
mPopWindow.setFocusable(true);
mPopWindow.setBackgroundDrawable(new BitmapDrawable());
mPopWindow.showAsDropDown(mBtnPop);
}
});
}
}
<?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:orientation="vertical"
>
<Button
android:id="@+id/btn_pop"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="POP"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
/>
</LinearLayout>

layout_pop.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorLittleBlue"
>
<TextView
android:id="@+id/tv_pop_good"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="好"
android:textSize="20sp"
android:gravity="center"
android:paddingTop="8dp"
android:paddingBottom="8dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/colorGreen"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="坏"
android:textSize="20sp"
android:gravity="center"
android:paddingTop="8dp"
android:paddingBottom="8dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/colorGreen"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="还行"
android:textSize="20sp"
android:gravity="center"
android:paddingTop="8dp"
android:paddingBottom="8dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/colorGreen"
/>
</LinearLayout>

本文详细探讨了Android中PopupWindow的使用,包括如何在PopupWindowActivity中集成和配置PopupWindow,以及它在页面布局file layout_pop.xml中的实现细节。
1万+

被折叠的 条评论
为什么被折叠?



