我需要像地图信息窗口这样的弹出消息

时间:2017-10-26 19:02:26

标签: android

我无法找到在地图中创建类似信息窗口的弹出窗口的方法。我必须用它来显示里面的三个按钮

  1. 稍后
  2. 这三个选项是可点击的。 弹出窗口将在回收器视图中以垂直方式显示上面的三个按钮。我创建了Recycler视图并弹出了视图,但是如何将其显示为带有clickabe的工具提示

1 个答案:

答案 0 :(得分:0)

你可以使用PopupWindow,它从API +23开始

<?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:background="#FFBBFFBB" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="Hello My Window" android:textSize="20sp" /> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="Button" android:textSize="20sp" /> </LinearLayout> 

package com.example.hellopopupwindow;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;

public class MainActivity extends Activity {

    private Context mContext = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mContext = this;

        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                showPopupWindow(view);
            }
        });
    }

    private void showPopupWindow(View view) {

        // A custom layout, as the display content
        View contentView = LayoutInflater.from(mContext).inflate(
                R.layout.pop_window, null);
        // Set the button click event
        Button button = (Button) contentView.findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "button is pressed",
                        Toast.LENGTH_SHORT).show();
            }
        });

        final PopupWindow popupWindow = new PopupWindow(contentView,
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);

        popupWindow.setTouchable(true);

        popupWindow.setTouchInterceptor(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                Log.i("mengdd", "onTouch : ");

                return false;
                // It returns true if the words, the touch event will be blocked
                // PopupWindow onTouchEvent interception is not called, so click on the external area cannot be dismiss
            }
        });

        // If you do not set the PopupWindow background, both the external region click or Back keys are not dismiss box
        // I think there is a bug API
        popupWindow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.selectmenu_bg_downward));

        // After setting the parameter to show
        popupWindow.showAsDropDown(view);

    }

}

enter image description here

相关问题