Java 如何在Android对话框的XML布局文件中指定正确的对话框大小?

Java 如何在Android对话框的XML布局文件中指定正确的对话框大小?,java,android,xml,string,performance,Java,Android,Xml,String,Performance,我使用XML布局文件创建了一个android对话框,如下所示: final Dialog dialog = new Dialog(this); dialog.setContentView(R.layout.enabledialog); dialog.setTitle("Define message keys and setup"); Switch locationSwitch = (Switch) dialog.findViewById(R.

我使用XML布局文件创建了一个android对话框,如下所示:

  final Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.enabledialog);
        dialog.setTitle("Define message keys and setup");

        Switch locationSwitch = (Switch) dialog.findViewById(R.id.locationSwitch);
        Switch blareSwitch = (Switch) dialog.findViewById(R.id.blareSwitch);

        final EditText locationEdit = (EditText) dialog.findViewById(R.id.locationEdit);
        final EditText blareEdit = (EditText) dialog.findViewById(R.id.blareEdit);

        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(dialog.getWindow().getAttributes());
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;

        dialog.show();

        dialog.getWindow().setAttributes(lp);
但是,我的XML文件的内容只占屏幕的一半,因此当我显示对话框时,我会显示额外的空间,这看起来不太好:

下面是对话框外观的示意图(R.layout.enabledialog):

我如何修剪这个额外的空间?在androidstudio中,布局编辑器假设XML文件占据整个屏幕,但实际上,我只想要一个小的弹出窗口

谢谢

拉希尔


编辑:这是我的布局资源文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#1dd1e9">


    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/locationSwitch"
        android:layout_marginTop="46dp"
        android:checked="false"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Blah"
        android:id="@+id/textView"
        android:textSize="30sp"
        android:layout_alignTop="@+id/locationSwitch"
        android:layout_centerHorizontal="true"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Blah Blah Blah"
        android:id="@+id/textView2"
        android:gravity="center"
        android:textSize="20sp"
        android:layout_below="@+id/locationSwitch"
        android:layout_centerHorizontal="true"
        />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/locationEdit"
        android:layout_below="@+id/textView2"
        android:layout_alignLeft="@+id/textView"
        android:layout_alignStart="@+id/textView"
        android:layout_alignRight="@+id/textView"
        android:layout_alignEnd="@+id/textView"
        android:singleLine = "true"
        android:imeOptions="actionDone" />

    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/blareSwitch"
        android:layout_marginTop="40dp"
        android:checked="false"
        android:layout_below="@+id/locationEdit"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Blah"
        android:id="@+id/textView3"
        android:textSize="30sp"
        android:layout_alignBottom="@+id/blareSwitch"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Blah Blah Blah"
        android:id="@+id/textView4"
        android:textSize="20sp"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/blareEdit"
        android:layout_below="@+id/textView4"
        android:layout_alignLeft="@+id/locationEdit"
        android:layout_alignStart="@+id/locationEdit"
        android:layout_alignRight="@+id/locationEdit"
        android:layout_alignEnd="@+id/locationEdit"
        android:singleLine = "true"
        android:imeOptions="actionDone" />
</RelativeLayout>

尝试将根
RelativeLayout
中的
fill\u parent
更改为
wrap\u content
,以及将Java代码中的
MATCH\u parent
更改为
wrap\u content
(或去掉整个
setAttributes()
位)

事实上,我在MATCH_PARENT上看到了它,因为当它是WRAP_内容时,内容是重叠的,不合适


那么这是一个单独的问题,您需要解决。首先,让对话框的大小大致符合您的要求。然后,使用一些检查工具(AS 2.2层次结构视图中的新工具,
uiautomatorviewer
等)开始确定如何修复布局以避免重叠。

尝试将布局参数高度更改为:


lp.height=WindowManager.LayoutParams.WRAP\u内容

根据xml文件中的要求更改主相对长度大小。如下图所示,指定所需的高度

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:background="#1dd1e9">

<Switch
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/locationSwitch"
    android:layout_marginTop="46dp"
    android:checked="false"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Blah"
    android:id="@+id/textView"
    android:textSize="30sp"
    android:layout_alignTop="@+id/locationSwitch"
    android:layout_centerHorizontal="true"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Blah Blah Blah"
    android:id="@+id/textView2"
    android:gravity="center"
    android:textSize="20sp"
    android:layout_below="@+id/locationSwitch"
    android:layout_centerHorizontal="true"
    />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/locationEdit"
    android:layout_below="@+id/textView2"
    android:layout_alignLeft="@+id/textView"
    android:layout_alignStart="@+id/textView"
    android:layout_alignRight="@+id/textView"
    android:layout_alignEnd="@+id/textView"
    android:singleLine = "true"
    android:imeOptions="actionDone" />

<Switch
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/blareSwitch"
    android:layout_marginTop="40dp"
    android:checked="false"
    android:layout_below="@+id/locationEdit"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Blah"
    android:id="@+id/textView3"
    android:textSize="30sp"
    android:layout_alignBottom="@+id/blareSwitch"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Blah Blah Blah"
    android:id="@+id/textView4"
    android:textSize="20sp"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/blareEdit"
    android:layout_below="@+id/textView4"
    android:layout_alignLeft="@+id/locationEdit"
    android:layout_alignStart="@+id/locationEdit"
    android:layout_alignRight="@+id/locationEdit"
    android:layout_alignEnd="@+id/locationEdit"
    android:singleLine = "true"
    android:imeOptions="actionDone" />
</RelativeLayout>

根据要求更改高度和宽度,不需要设置属性(lp);在Java中。
如果您正在设置,确保您考虑XML相对高度和宽度

,我通常以编程方式设置<代码> LayoutParams < /代码>。我想你可以在
onResume
中使用下面的代码尝试
DialogFragment

/**
 * Setup position of dialog 
 */
protected void setupDialogPosition() {
    // setup the dialog
    WindowManager.LayoutParams p = getDialog().getWindow().getAttributes();
    p.width = ViewGroup.LayoutParams.MATCH_PARENT;
    p.y = 0;
    // change height of dailog
    if (mHeight == ViewGroup.LayoutParams.MATCH_PARENT) {
        p.height = mScreenHeight - p.y;
        if (isStatusBarVisible()) {
            p.height -= getStatusBarHeight();
        }
        } else {
        p.height = ViewGroup.LayoutParams.WRAP_CONTENT;
    }
    getDialog().getWindow().setGravity(mGravity);
    getDialog().getWindow().setAttributes(p);
}

将RelativeLayout更改为LinearLayout:

<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
</LinearLayout>

直接从资源值转换:

int width = getResources().getDimensionPixelSize(R.dimen.popup_width);
int height = getResources().getDimensionPixelSize(R.dimen.popup_height);        
getDialog().getWindow().setLayout(width, height);
现在,在对话框的布局中
match_parent

android:layout_width="match_parent"
android:layout_height="match_parent"

android:layout_width="match_parent"
android:layout_height="match_parent"

希望这对您有所帮助。

在您的相对布局中指定布局高度和布局宽度,不要将其设置为填充父级。 例如:


创建自定义对话框,如下所示

public class MyDialog extends Dialog {

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

        getWindow().setAttributes(lp);
    }

    public MyDialog(Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog);
    }
}
new MyDialog(ActivityContext).show();
请看onAttachedToWindow()的重写方法,这里是您的技巧。此方法在onCreate()方法之前被调用。因此,您可以在创建对话框之前覆盖windows属性(宽度和高度)

从活动中调用此对话框,如下所示

public class MyDialog extends Dialog {

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

        getWindow().setAttributes(lp);
    }

    public MyDialog(Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog);
    }
}
new MyDialog(ActivityContext).show();
还可以在dialog.xml文件中进行一些修改

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical">

    <Switch
        android:id="@+id/locationSwitch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="46dp"
        android:checked="false" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/locationSwitch"
        android:layout_centerHorizontal="true"
        android:text="Blah"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/locationSwitch"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:text="Blah Blah Blah"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/locationEdit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/textView"
        android:layout_alignLeft="@+id/textView"
        android:layout_alignRight="@+id/textView"
        android:layout_alignStart="@+id/textView"
        android:layout_below="@+id/textView2"
        android:layout_marginBottom="50dp"
        android:imeOptions="actionDone"
        android:singleLine="true" />

</LinearLayout>


编辑版面文件如果您需要任何额外的定制

请发布版面资源文件。您可能还希望将Java代码中的
MATCH_PARENT
更改为
WRAP_CONTENT
。@commonware Edited:)@commonware我实际上在
MATCH_PARENT
上有它,因为当它是
WRAP_CONTENT
时,内容重叠且不合适。好的,我已将所有的
匹配父项
更改为
包装内容
,重叠仍在继续。我如何获得/使用您提到的那些检查工具?Thanks@RuchirBaronia:
uiautomatorviewer
是SDK中的命令行工具。层次结构视图位于Android设备监视器中(可从Android Studio主菜单中的工具>Android获得)。这两个方面都在文档中有所涉及,至少是很简单。基本上,它们让你看到从设备加载的UI的实际内容,因此,您可以在实际应用规则时将其可视化。我如何知道哪个高度?请检查父级相对中的代码。我已将高度指定为100dp。使用该高度并根据要求进行更改。更改父级布局高度检查代码并进行跟踪和错误。。