Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android中的对话框位置_Android - Fatal编程技术网

android中的对话框位置

android中的对话框位置,android,Android,我正在创建一个商店地图,其中图像来自服务器。我有产品项目具体位置的坐标。我在画布上绘制的不同标记上显示对话框。 当我在makers上单击时,对话框显示得非常完美,但当我滚动图像并再次单击标记时,对话框位置也会每次更改。是否有任何解决方案可以完美定位图像滚动条上的对话框? 多谢各位 main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http

我正在创建一个商店地图,其中图像来自服务器。我有产品项目具体位置的坐标。我在画布上绘制的不同标记上显示对话框。 当我在makers上单击时,对话框显示得非常完美,但当我滚动图像并再次单击标记时,对话框位置也会每次更改。是否有任何解决方案可以完美定位图像滚动条上的对话框? 多谢各位

main.xml

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical" >

   <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:background="#2567B3" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="First Floor"
            android:textColor="@color/white"
            android:textSize="20sp"
            android:textStyle="bold"   />

        <Button
            android:id="@+id/back1"
            android:layout_width="25dp"
            android:layout_height="30dp"
            android:layout_marginLeft="5dp"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:background="@drawable/back" />

        <ImageView
            android:id="@+id/product_list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerHorizontal="true"
            android:layout_marginRight="10dp"
            android:background="@drawable/selector"
             />
    </RelativeLayout>

    <HorizontalScrollView
        android:id="@+id/horizontalScrollView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_below="@+id/relativeLayout1">
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <com.shopping.PaintView
                android:id="@+id/paint_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/test" />
        </RelativeLayout>
    </HorizontalScrollView>

</RelativeLayout>

MainActivity.java

    public class MainActivity extends BaseActivity implements OnTouchListener {

    static float mX,mY,mX1,mY1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Getting reference to PaintView
        PaintView paintView = (PaintView) findViewById(R.id.paint_view);
        paintView.setOnTouchListener(this);
        backclick();
    }

    @SuppressLint("InlinedApi")
    @Override
    public boolean onTouch(View v,MotionEvent event) {
        // TODO Auto-generated method stub
        final Dialog dialogMarketList = new Dialog(MainActivity.this);

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View viewList = inflater.inflate(R.layout.custom_dialog,null);
        dialogMarketList.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialogMarketList.setContentView(viewList);

        Window window = dialogMarketList.getWindow();
        WindowManager.LayoutParams wmlp = window.getAttributes();
        wmlp.gravity = Gravity.START | Gravity.TOP;
        wmlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        window.setAttributes(wmlp);

        TextView header = (TextView) viewList.findViewById(R.id.header);




        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            // // Getting X & Y coordinate
            int offers[] = { 0, 1, 1, 0, 1, 0, 1, 0, 1, 0 };
            int pointx[] = { 47, 338, 396, 52, 49, 387, 468, 455, 217, 231 };
            int pointy[] = { 77, 59, 297, 294, 564, 534, 56, 407, 478, 187 };
            int distance = 35;
             mX = event.getX();
             mY = event.getY();



            for (int i = 0; i < 10; i++) { 

                if (mX > pointx[i] - distance && mX < pointx[i] + distance && mY >pointy[i] - distance && mY < pointy[i] + distance) {


                    if (offers[i] == 0){
                        header.setText("floor: 1" + "\nShelf: Top"
                                + "\nPosition: Right");
                        wmlp.x = (int) mX; // x position
                        wmlp.y = (int) mY; // y position
                    }

                    else {
                            header.setText("floor: 2" + "\nShelf: Top"
                                + "\nPosition: Right" + "\nOffer:10% discount");
                            wmlp.x = (int) mX; // x position
                            wmlp.y = (int) mY; // y position
                    }

                    dialogMarketList.show();

                }
            }
            break;
        }
        return false;
    }
    public class PaintView extends View {
        int offers[]={0,1,1,0,1,0,1,0,1,0};
        Paint mPaint;
        float mX[]={47,338,396,52,49,387,468,455,217,231};
        float mY[]={77,59,297,294,564,534,56,407,478,187};  
        TextView mTVCoordinates;
        // ---add the marker---
        Bitmap marker1;
        Bitmap marker2;
        public PaintView(Context context,AttributeSet attributeSet){
            super(context,attributeSet);

        /** Initializing the variables */
        mPaint = new Paint();
        mTVCoordinates = null;

        marker1 = BitmapFactory.decodeResource(getResources(),R.drawable.blue);
        marker2 = BitmapFactory.decodeResource(getResources(),R.drawable.red);  
    }   
    @Override
    protected void onDraw(Canvas canvas) {      
        super.onDraw(canvas);



    for(int i=0;i<10;i++)
    {       
        if(offers[i]==0)
        {   canvas.drawBitmap(marker1,mX[i], mY[i], null);
            invalidate();
        }
        else
        {   canvas.drawBitmap(marker2, mX[i], mY[i], null);
            invalidate();   
        }
    }
    }
public类MainActivity扩展了BaseActivity实现的OnTouchListener{
静态浮动mX,mY,mX1,mY1;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取对PaintView的引用
PaintView PaintView=(PaintView)findViewById(R.id.paint\U视图);
paintView.setOnTouchListener(此);
backclick();
}
@SuppressLint(“InlinedApi”)
@凌驾
公共布尔onTouch(视图v,运动事件){
//TODO自动生成的方法存根
最终对话框dialogMarketList=新建对话框(MainActivity.this);
LayoutFlater充气器=(LayoutFlater)getSystemService(Context.LAYOUT\u充气器\u服务);
查看视图列表=充气机。充气(R.layout.custom_对话框,空);
dialogMarketList.requestWindowFeature(Window.FEATURE\u NO\u TITLE);
dialogMarketList.setContentView(viewList);
Window=dialogMarketList.getWindow();
WindowManager.LayoutParams wmlp=window.getAttributes();
wmlp.gravity=gravity.START | gravity.TOP;
wmlp.flags&=~WindowManager.LayoutParams.FLAG\u DIM\u后面;
设置属性(wmlp);
TextView头=(TextView)viewList.findViewById(R.id.header);
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
////获取X&Y坐标
int提供[]={0,1,1,0,1,0,1,1,0,1,0};
int pointx[]={47338396,52,49387468455217231};
int pointy[]={77,59,297,294,564,534,56,407,478,187};
整数距离=35;
mX=event.getX();
mY=event.getY();
对于(int i=0;i<10;i++){
if(mX>pointx[i]-distance&&mXpointy[i]-distance&&mY
PaintView.java

    public class MainActivity extends BaseActivity implements OnTouchListener {

    static float mX,mY,mX1,mY1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Getting reference to PaintView
        PaintView paintView = (PaintView) findViewById(R.id.paint_view);
        paintView.setOnTouchListener(this);
        backclick();
    }

    @SuppressLint("InlinedApi")
    @Override
    public boolean onTouch(View v,MotionEvent event) {
        // TODO Auto-generated method stub
        final Dialog dialogMarketList = new Dialog(MainActivity.this);

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View viewList = inflater.inflate(R.layout.custom_dialog,null);
        dialogMarketList.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialogMarketList.setContentView(viewList);

        Window window = dialogMarketList.getWindow();
        WindowManager.LayoutParams wmlp = window.getAttributes();
        wmlp.gravity = Gravity.START | Gravity.TOP;
        wmlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        window.setAttributes(wmlp);

        TextView header = (TextView) viewList.findViewById(R.id.header);




        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            // // Getting X & Y coordinate
            int offers[] = { 0, 1, 1, 0, 1, 0, 1, 0, 1, 0 };
            int pointx[] = { 47, 338, 396, 52, 49, 387, 468, 455, 217, 231 };
            int pointy[] = { 77, 59, 297, 294, 564, 534, 56, 407, 478, 187 };
            int distance = 35;
             mX = event.getX();
             mY = event.getY();



            for (int i = 0; i < 10; i++) { 

                if (mX > pointx[i] - distance && mX < pointx[i] + distance && mY >pointy[i] - distance && mY < pointy[i] + distance) {


                    if (offers[i] == 0){
                        header.setText("floor: 1" + "\nShelf: Top"
                                + "\nPosition: Right");
                        wmlp.x = (int) mX; // x position
                        wmlp.y = (int) mY; // y position
                    }

                    else {
                            header.setText("floor: 2" + "\nShelf: Top"
                                + "\nPosition: Right" + "\nOffer:10% discount");
                            wmlp.x = (int) mX; // x position
                            wmlp.y = (int) mY; // y position
                    }

                    dialogMarketList.show();

                }
            }
            break;
        }
        return false;
    }
    public class PaintView extends View {
        int offers[]={0,1,1,0,1,0,1,0,1,0};
        Paint mPaint;
        float mX[]={47,338,396,52,49,387,468,455,217,231};
        float mY[]={77,59,297,294,564,534,56,407,478,187};  
        TextView mTVCoordinates;
        // ---add the marker---
        Bitmap marker1;
        Bitmap marker2;
        public PaintView(Context context,AttributeSet attributeSet){
            super(context,attributeSet);

        /** Initializing the variables */
        mPaint = new Paint();
        mTVCoordinates = null;

        marker1 = BitmapFactory.decodeResource(getResources(),R.drawable.blue);
        marker2 = BitmapFactory.decodeResource(getResources(),R.drawable.red);  
    }   
    @Override
    protected void onDraw(Canvas canvas) {      
        super.onDraw(canvas);



    for(int i=0;i<10;i++)
    {       
        if(offers[i]==0)
        {   canvas.drawBitmap(marker1,mX[i], mY[i], null);
            invalidate();
        }
        else
        {   canvas.drawBitmap(marker2, mX[i], mY[i], null);
            invalidate();   
        }
    }
    }
public类PaintView扩展视图{
int提供[]={0,1,1,0,1,0,1,0,1,1,0};
油漆;
浮动mX[]={47338396,52,493874684455217231};
浮动我的[]={77,59297294564534,56407478187};
文本视图MTV坐标;
//---添加标记---
位图标记1;
位图标记2;
公共画图视图(上下文上下文,属性集属性集){
super(上下文、属性集);
/**初始化变量*/
mPaint=新油漆();
mTVCoordinates=null;
marker1=BitmapFactory.decodeResource(getResources(),R.drawable.blue);
marker2=BitmapFactory.decodeResource(getResources(),R.drawable.red);
}   
@凌驾
受保护的void onDraw(画布){
super.onDraw(帆布);

对于(int i=0;i请尝试在屏幕底部显示此对话框:

Dialog dlg = <code to create custom dialog>;

Window window = dlg.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();

wlp.gravity = Gravity.BOTTOM;
wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(wlp);
Dialog-dlg=;
Window Window=dlg.getWindow();
WindowManager.LayoutParams wlp=window.getAttributes();
wlp.gravity=gravity.BOTTOM;
wlp.flags&=~WindowManager.LayoutParams.FLAG\u DIM\u后面;
设置属性(wlp);

如果您需要,此代码还可以防止android将对话框的背景调暗。您应该能够更改重力参数以将对话框移动约

尝试将对话框的定位点设置为您的标记。Google maps还使用定位点显示标记的标记窗口

这里有一个锚定的方法

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);

button.setLayoutParams(params); 

希望对你有帮助

你需要详细说明这个问题。上面的解释毫无意义。