4

我一直在移动对话框。在这里,我发布了我的完整源代码和屏幕截图。为什么我的对话框丢失了一部分。

代码 :

public class MoveDialogBox extends Activity 
{
    Dialog dialog;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                showDialog(1);
            }
        });
    }   
    @Override
    protected Dialog onCreateDialog( int id ) 
    { 
        switch ( id ) 
        {
            case 1:
                AlertDialog.Builder builder = null;
                Context mContext = this;
                LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
                View layout = inflater.inflate(R.layout.info, (ViewGroup) findViewById(R.id.root));
                Button exit = (Button) layout.findViewById(R.id.exit);
                exit.setOnClickListener(new View.OnClickListener() 
                {
                    @Override
                    public void onClick(View v) 
                    {
                        dismissDialog(1);
                    }
                });
                final LinearLayout LL = (LinearLayout) layout.findViewById(R.id.root);
                LL.bringToFront();
                LL.setOnTouchListener(new View.OnTouchListener() 
                {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) 
                    {
                        double X = event.getX();
                        double Y = event.getY();
                        String.valueOf(event.getX() + "x" + String.valueOf(event.getY()));
                        Toast.makeText(getBaseContext(), X + " : " + Y, Toast.LENGTH_SHORT).show();
                        LL.bringToFront();
                        LL.setX((float) X);
                        LL.setY((float) Y);
                        return false;
                    }
                });
                builder = new AlertDialog.Builder(mContext);
                builder.setView(layout);
                dialog = builder.create();
                dialog.setCanceledOnTouchOutside(true);
                break;
        }
        return dialog;
    }
}

XML 代码:info.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/holo_blue_light"
    android:orientation="horizontal" >
    <TextView 
        android:id="@+id/about_Title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_weight="1"
        android:textSize="25dip"
        android:textColor="@android:color/white"
        android:textStyle="bold|italic"
        android:text="Android" />
    <Button
        android:id="@+id/exit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Exit" />    
</LinearLayout>

XML 代码:main.xml

<LinearLayout 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"
    android:background="@drawable/androidimage" >
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Open Dialog Box" />
</LinearLayout>

屏幕截图:1
在此处输入图像描述
屏幕截图:2
在此处输入图像描述
屏幕截图:3
在此处输入图像描述
请帮助一些人如何在整个屏幕中移动对话框。

我在哪里弄错了。怎么解决。

4

1 回答 1

4

您正在移动对话框内的布局,您需要移动对话框本身。

WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
layoutParams.gravity = Gravity.TOP | Gravity.LEFT;
layoutParams.x = youNewX;
layoutParams.y = yourNewY;
dialog.getWindow().setAttributes(layoutParams);
于 2012-11-23T14:58:20.473 回答