猿问

如何在Android中创建弹出窗口(PopupWindow)

要创建一个简单的工作PopupWindow,我们需要执行以下操作:


popup_example.xml:


<?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:orientation="vertical"

        android:padding="10dip"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content">


        <TextView         

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:layout_marginTop="10dip"

            android:text="Test Pop-Up" />


    </LinearLayout>

Java代码


LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 


PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup_example, null, false),100,100, true);


pw.showAtLocation(this.findViewById(R.id.main), Gravity.CENTER, 0, 0);

我的要求是我需要一个


<TEXTVIEW android:layout_height="wrap_content" android:layout_width="fill_parent" />

和一个


<BUTTON android:id="@+id/end_data_send_button" android:text="Cancel"/>

在我的popup_example.xml。如何在Java代码中处理这两个组件?


慕仙森
浏览 885回答 3
3回答

catspeake

在这里,我给您一个演示示例。看到并根据您的需要对其进行自定义。public class ShowPopUp extends Activity {&nbsp; &nbsp; &nbsp;PopupWindow popUp;&nbsp; &nbsp; &nbsp;LinearLayout layout;&nbsp; &nbsp; &nbsp;TextView tv;&nbsp; &nbsp; &nbsp;LayoutParams params;&nbsp; &nbsp; &nbsp;LinearLayout mainLayout;&nbsp; &nbsp; &nbsp;Button but;&nbsp; &nbsp; &nbsp;boolean click = true;&nbsp; &nbsp; &nbsp;@Override&nbsp; &nbsp; &nbsp;public void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; popUp = new PopupWindow(this);&nbsp; &nbsp; &nbsp; layout = new LinearLayout(this);&nbsp; &nbsp; &nbsp; mainLayout = new LinearLayout(this);&nbsp; &nbsp; &nbsp; tv = new TextView(this);&nbsp; &nbsp; &nbsp; but = new Button(this);&nbsp; &nbsp; &nbsp; but.setText("Click Me");&nbsp; &nbsp; &nbsp; but.setOnClickListener(new OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp;public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; if (click) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;popUp.update(50, 50, 300, 80);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;click = false;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;popUp.dismiss();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;click = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; params = new LayoutParams(LayoutParams.WRAP_CONTENT,&nbsp; &nbsp; &nbsp; &nbsp; LayoutParams.WRAP_CONTENT);&nbsp; &nbsp; &nbsp; layout.setOrientation(LinearLayout.VERTICAL);&nbsp; &nbsp; &nbsp; tv.setText("Hi this is a sample text for popup window");&nbsp; &nbsp; &nbsp; layout.addView(tv, params);&nbsp; &nbsp; &nbsp; popUp.setContentView(layout);&nbsp; &nbsp; &nbsp; // popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);&nbsp; &nbsp; &nbsp; mainLayout.addView(but, params);&nbsp; &nbsp; &nbsp; setContentView(mainLayout);&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }希望这能解决您的问题。
随时随地看视频慕课网APP

相关分类

Android
我要回答