单击按钮时显示列表视图:Android

发布于 2024-09-28 04:38:48 字数 224 浏览 1 评论 0原文

我正在尝试在单击按钮时实现下拉列表。

因此,我在导航栏(nav.xml)中有一个文本视图和一个按钮以及相应的列表视图。该导航栏包含在另一个页面( products.xml )中

,当单击按钮时,我会在按钮正下方获得列表视图(这就是我想要实现的),但它是我向下移动当前页面上的所有内容,甚至放置在导航栏中的文本视图向下移动。

我对 Android 完全陌生,任何示例或实现它的方法 ???

I am trying to implement a drop down list when a button is clicked.

So, I have a text view and a button in a navigation bar(nav.xml) and a corresponding list view. This navigation bar is included in another page( products.xml)

when the button is clicked i get the list view right below the button(which is what i want to acheive) but its my moving all the contents on the current page downwards, even the text view which is placed in nav bar moved downwards.

I am totally new to Android, any sample examples or a way how to achieve it
???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

度的依靠╰つ 2024-10-05 04:38:48

听起来你需要一个Spinner。它相当于 Android 的下拉列表。您可以在此处找到示例。

Sounds like you need a Spinner. It's the equivalent of a drop down list for Android. You can find an example here.

与君绝 2024-10-05 04:38:48

因此,根据我们的需要,我们需要使用 ListPopupWindow。
官方说明链接:

http://developer.android.com/reference/android/widget/ ListPopupWindow.html

让我们深入研究代码:

我们有自己的方法:

    public void downloadBtnSelected(View anchor) {
            final ListPopupWindow lpw = new ListPopupWindow(this);
            String[] data = { ".png", ".pdf", ".jpg", ".jpeg" };
            PopupAdapter pa = new PopupAdapter(data, this);
            lpw.setAdapter(pa);

            //setting up an anchor view
            lpw.setAnchorView(anchor);

            //Setting measure specifications. I'v used this mesure specs to display my
            //ListView as wide as my anchor view is
            lpw.setHeight(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
            lpw.setWidth(anchor.getRight() - anchor.getLeft());

            // Background is needed. You can use your own drawable or make a 9patch.
            // I'v used a custom btn drawable. looks nice.
            lpw.setBackgroundDrawable(this.getResources().getDrawable(
                    android.R.drawable.btn_default));

            // Offset between anchor view and popupWindow
            lpw.setVerticalOffset(3); 

            lpw.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {
                              /// Our action.....
                    lpw.dismiss();

                }
            });
            lpw.show();

        }

以及带有 onClickListener 的按钮来调用此方法:

Button btn = new Button(this);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                downloadBtnSelected(v);

            }
        });

我们传递 View v 参数作为我们的锚点,以便让我们的 PopupWindow 知道在哪里显示自己。如果下面有足够的空间,它将显示在锚视图的左下角。如果没有 - 它将显示

So, for our need we need to use ListPopupWindow.
The link to official description:

http://developer.android.com/reference/android/widget/ListPopupWindow.html

Let's dive in the code:

we have our own method:

    public void downloadBtnSelected(View anchor) {
            final ListPopupWindow lpw = new ListPopupWindow(this);
            String[] data = { ".png", ".pdf", ".jpg", ".jpeg" };
            PopupAdapter pa = new PopupAdapter(data, this);
            lpw.setAdapter(pa);

            //setting up an anchor view
            lpw.setAnchorView(anchor);

            //Setting measure specifications. I'v used this mesure specs to display my
            //ListView as wide as my anchor view is
            lpw.setHeight(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
            lpw.setWidth(anchor.getRight() - anchor.getLeft());

            // Background is needed. You can use your own drawable or make a 9patch.
            // I'v used a custom btn drawable. looks nice.
            lpw.setBackgroundDrawable(this.getResources().getDrawable(
                    android.R.drawable.btn_default));

            // Offset between anchor view and popupWindow
            lpw.setVerticalOffset(3); 

            lpw.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {
                              /// Our action.....
                    lpw.dismiss();

                }
            });
            lpw.show();

        }

and the button with an onClickListener to call this method:

Button btn = new Button(this);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                downloadBtnSelected(v);

            }
        });

we pass the View v argument as our anchor, in order to let our PopupWindow to know where to display itself. It will be displayed in the bottom-left corner of our anchor view if there is enough room below. If not- it will displa

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文