我正在为我的Android应用程序制作一个简单的自定义对话框,只显示一个搜索栏。然而,这项简单任务的复杂性让我疯狂。
我的对话框布局如下:
<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”时,会发生更奇怪的事情:
答案 0 :(得分:9)
默认情况下,Dialog
会为标题留出空间,即使您没有设置标题(使用d.setTitle()
)。
您可以设置标题以填充空格,也可以请求Dialog
没有标题
以下是如何请求无标题设置的示例。
Dialog d = new Dialog(this);
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setContentView(R.layout.custom_dialog);
如果没有标题,您的SeekBar
将按预期显示。
答案 1 :(得分:0)
尝试在父线性布局上添加固定高度。类似的东西:
android:layout_height="50px"