2012-08-13 41 views
3

我一直在移動對話框。在這裏,我張貼了我的完整源代碼和屏幕截圖。爲什麼我的對話框已經丟失了一些部分。對話框在android中移動?

代碼:

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個
enter image description here
屏幕截圖:2
enter image description here
截屏:3
enter image description here
請幫某一個如何移動對話框在整個屏幕。

我在哪裏犯了錯。如何解決。

+0

發佈的XML文件也使用的是設置視圖對話框 – 2012-08-13 08:38:37

+0

沒有截屏附 – juned 2012-08-13 09:03:41

+0

@juned請比較我的第二和第三屏幕快照什麼。 – Sekar 2012-08-13 09:15:18

回答

3

您正在移動對話框內的佈局,您需要移動對話框本身。

WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes(); 
layoutParams.gravity = Gravity.TOP | Gravity.LEFT; 
layoutParams.x = youNewX; 
layoutParams.y = yourNewY; 
dialog.getWindow().setAttributes(layoutParams); 
+0

「對話框沒有任何Touch事件偵聽器...」http://stackoverflow.com/questions/10948756/how-to-move-the-custom-dialog-box – samosaris 2013-05-10 16:18:24