1

我有一个包含以下内容的对话框:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="25dp" >
        <LinearLayout
           ...
        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/closebtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/scrollview"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

我希望对话框垂直调整大小以尽可能多地适应 LinearLayout 的内容。但是,我发现,如果 LinearLayout 的内容太高,则 ScrollView 会填充对话框,并且关闭按钮会被推过底部并且不可见。

我尝试的一件事是制作 Button layout_alignParentBottom="true" 并制作 ScrollView layout_above="@+id/closebtn",但随后对话框总是拉伸以垂直填充整个屏幕,即使 LinearLayout 的内容真的短的。

4

3 回答 3

0

我最终将我的 RelativeLayout 更改为 LinearLayout 以使其正常工作。我最初没有这样做,因为当我尝试它时,它给了我一个非常高而瘦的对话框(内部 LinearLayout 包含 ImageViews 和 TextViews)。但是,令人惊讶的是,将我的所有视图从 width=fill_parent 更改为 wrap_content 会导致对话框水平拉伸以填充屏幕,这正是我想要的,并且高度表现正确。

于 2012-06-25T06:20:19.657 回答
0

在您致电后:

AlertDialog dialog = builder.show();    

然后调用:

fixScrollViewHeight(scrollView);

//

private void fixScrollViewHeight(ScrollView scrollView) {
    int screenHeight = activity.getWindowManager().getDefaultDisplay().getHeight();
    LayoutParams lp = scrollView.getLayoutParams();
    lp.height = screenHeight / 3;
    scrollView.setLayoutParams(lp);
}
于 2013-01-07T17:15:43.677 回答
0

一种解决方案是以编程方式获取屏幕高度并手动指定滚动视图的高度。

获取屏幕尺寸(以像素为单位):

以像素为单位获取屏幕尺寸

问题是您的按钮的高度将是每个屏幕上不同数量的像素,基于像素密度,但您的按钮将是一定数量的 DP。

在您的 XML 中,为 DP 中的按钮设置高度。

然后计算你的按钮有多少像素高。请参阅:Android 上的“px”、“dp”、“dip”和“sp”有什么区别?

然后,您可以使用屏幕高度减去按钮的高度来确定您可以在按钮的布局参数中设置的大小。

LayoutParams params = findViewById(R.id.your_scroll_view).getLayoutParams();
params.height = screenHeight - buttonHeight;
于 2012-06-25T00:59:08.280 回答