在android活动的右上角显示对话框

时间:2014-03-06 06:43:12

标签: android android-layout

如何在Android活动的右上角显示对话框。

以下是我的对话框代码

item.xml

 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="150dp"
    android:layout_height="wrap_content" >

    <com.CustomToggleButton
        android:id="@+id/toggleButton2"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:textOff="@string/off_txt"
        android:textOn="@string/on_txt" />

</TableLayout>

ActivityMy.java

 dialog_box = new Dialog(ActivityMy.this);
                        dialog_box.setContentView(R.layout.item);
                        dialog_box.show();

2 个答案:

答案 0 :(得分:3)

喜欢这个

Window window = dialog.getWindow();
    WindowManager.LayoutParams wlp = window.getAttributes();
    wlp.gravity = Gravity.TOP | Gravity.RIGHT;

    wlp.width = LayoutParams.MATCH_PARENT;
    wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
    window.setAttributes(wlp);

答案 1 :(得分:0)

如果您想要一些余量,请使用

`

Window window = dialog.getWindow();
    WindowManager.LayoutParams wlp = window.getAttributes();
    wlp.gravity = Gravity.TOP | Gravity.RIGHT;
    wlp.width = LayoutParams.MATCH_PARENT;
    wlp.y=100   //if you want give margin from top
    wlp.x=100     //if you want give margin from left
    wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
    window.setAttributes(wlp);