理解Android对话框布局

3
我正在为我的安卓应用制作一个简单的自定义对话框,只显示一个滑动条。然而,这个简单任务的复杂性让我很烦恼。
对话框的布局如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp">

<SeekBar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dialogVolumeSlider"
    android:layout_width="225dp"
    android:layout_height="wrap_content"/>

</LinearLayout>

对话框是在代码中创建的:
Dialog d = new Dialog(this);
d.setContentView(R.layout.custom_dialog);
return d;

“而不是简单地用一个盒子包裹拖动条,我得到了这个幻影空间从某个地方出现:

问题截图

问题在哪里? 我已经尝试修改


d.getWindow().getAttributes().height

但是这也会带来额外的问题。
谢谢任何帮助!
编辑:当我给我的LinearLayout的layout_height分配了一个固定的“50dp”时,奇怪的事情发生了:
2个回答

9

默认情况下,即使您没有设置标题(使用 d.setTitle()),Dialog 也会留出标题的空间。

您可以设置一个标题来填充这个空间,或者要求 Dialog 没有标题。
以下是如何请求不带标题设置的示例:

Dialog d = new Dialog(this);
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setContentView(R.layout.custom_dialog);

没有标题的情况下,您的SeekBar将会出现与您预期的相同。

0

尝试在您的父线性布局上设置固定高度。例如:

android:layout_height="50px"

请查看编辑,它只是切断了线性布局,但仍将其推向框的底部。 - cemulate

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接