android中的对话位置

时间:2014-07-21 10:53:14

标签: android

我正在创建一个商店地图,其中图像来自服务器。我有产品项目的具体位置的协调。我正在通过画布绘制的不同标记上显示对话框。 当我点击制作者时,对话框显示完美,但是当我滚动图像和放大时再次单击标记,对话框位置也会每次更改。有没有解决方案在图像滚动上完美定位对话框? 谢谢。

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;
    }

PaintView.java

    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();   
        }
    }
    }

2 个答案:

答案 0 :(得分:0)

尝试此操作以在屏幕底部显示对话框:

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);

此代码还可以防止android在需要时调暗对话框的背景。您应该能够更改重力参数以移动关于

的对话框

答案 1 :(得分:0)

尝试将对话框的锚点设置为您的标记.Google地图也使用锚点显示标记的标记窗口。

这是设置锚点的方法。

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); 

希望有所帮助