单击菜单项时如何获得弹出窗口?

时间:2019-03-06 13:57:21

标签: java android xml

我是Android App开发和Java的新手,并且遇到以下问题: 我在操作栏中的菜单内有一个“关于”选项。我希望单击“关于”选项时显示弹出窗口,显示应用程序版本和开发人员详细信息。由于我没有Android应用程序开发和Java的经验(尽管我仍在学习),请您能帮我!

popup_window.xml

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginStart="133dp"
    android:layout_marginTop="126dp"
    android:text="@string/appVersion" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignStart="@+id/textView1"
    android:layout_alignParentTop="true"
    android:layout_marginTop="156dp"
    android:text="@string/developer" />
</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    // create an action bar button
    @Override
    public boolean onCreateOptionsMenu(android.view.Menu menu) {
        getMenuInflater().inflate(R.menu.mymenu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    // handle button activities
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.about) {
            // about app page
            onButtonShowPopupWindowClick();
        }
        return super.onOptionsItemSelected(item);
    }

    public void onButtonShowPopupWindowClick(View view) {
        //inflate layout of popup window
        LayoutInflater inflater = (LayoutInflater)  getSystemService(LAYOUT_INFLATER_SERVICE);
        View popupView = inflater.inflate(R.layout.popup_window, null);

        // creating the popup window
        int width = LinearLayout.LayoutParams.WRAP_CONTENT;
        int height = LinearLayout.LayoutParams.WRAP_CONTENT;
        boolean focusable = true; // lets taps outside the popup also dismiss it
        final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);

        // show the popup window
        // which view you pass in doesn't matter, it is only used for the window tolken
        popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);

        // dismiss the popup window when touched
        popupView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                popupWindow.dismiss();
                return true;
            }
        });
    }
}

4 个答案:

答案 0 :(得分:0)

您应该尝试使用AlertDialog:

new AlertDialog.Builder(context)
                .setView(R.layout.popup_window)
                .create().show();

答案 1 :(得分:0)

您可以通过为菜单项提供xml中的actionViewClass属性来实现此目的,然后您将能够获取所需的数据透视图。该代码将是这样的

<item
    android:id="@+id/about"
    android:showAsAction="ifRoom"
    android:actionViewClass="android.widget.Button"
    />

在您的OnCreateOptionsMenu中执行此操作

public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.menu_search, menu);
    about= (ImageButton) menu.findItem(R.id.menu_find).getActionView();
    about.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            onButtonShowPopupWindowClick(v);
        }
    });
    return true;
}

答案 2 :(得分:0)

我认为: https://developer.android.com/guide/topics/ui/dialogs 是您入门的良好指南。它还通过android中的弹出窗口展示了良好做法

希望这会有所帮助

PS:在底部,您将找到一个示例,其中包含您需要的自定义弹出窗口

答案 3 :(得分:0)

您可以在自定义视图中使用[HardcodedDebugMode]。使用自定义视图,您可以通过XML设计AlertDialog

AlertDialog

您必须创建一个名为 yourlayout 的XML文件,并在此布局中设计对话框。

void的参数public void onButtonShowPopupWindowClick(Context context) { LayoutInflater layoutinflater = getLayoutInflater; View view1 = layoutinflater.inflate(R.layout.yourlayout, null); AlertDialog.Builder builder1 = new AlertDialog.Builder(context); builder1.setCancelable(true); builder1.setView(view1); AlertDialog alert = builder1.create(); alert.show(); } 必须为context