在对话框中使用ScrollView会导致底部视图消失

9

我有一个对话框,其中包含一个列表(一组位于LinearLayout内的TextView)。该列表位于ScrollView内。以下是布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ProgressBar
        android:id="@+id/delete_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone" />

    <ScrollView
        android:id="@+id/filename_scroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/filename_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>

    <View
        android:id="@+id/horisontal_separator"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray" />

    <LinearLayout
        android:id="@+id/button_container"
        android:layout_width="match_parent"
        android:layout_height="48dp" >

        <Button
            android:id="@+id/load_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/button_load"
            android:gravity="center"
            android:layout_weight="1" />

        <View
            android:layout_width="1dp"
            android:layout_height="fill_parent"
            android:background="@android:color/darker_gray" />

        <Button
            android:id="@+id/delete_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/button_delete"
            android:gravity="center"
            android:layout_weight="1" />
    </LinearLayout>

</LinearLayout>

只有几个项目在列表中时,它看起来是这样的: A few items on the list 但当屏幕上有更多内容无法显示(需要滚动时),我的按钮会被推到屏幕下方。当向下滚动到底部时,它看起来是这样的: A lot of items on the list 我需要包含按钮的LinearLayout作为页脚,它不应该滚动或消失。虽然我尝试过调整布局高度和权重,但没有效果。

你尝试过将ListView设置为固定高度,例如200dip吗? - Demonick
我没有实际的 ListView,但是改变 LinearLayout 的高度并没有什么区别。如果我改变 ScrollView 的高度,它只会再次推出按钮。 - j0ntech
3个回答

25

尝试在scrollView中进行更改

<ScrollView
    android:id="@+id/filename_scroll"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_height="0dp" >

这个完美地运作了。你能否解释一下为什么以及如何发生的? - Sakshi Gupta
这对我也起作用了!很想知道为什么这样做有效。 - VIN

0

我认为你应该明确地提及ScrollView的layout_height,例如:

<ScrollView
    android:id="@+id/filename_scroll"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" > ////say  android:layout_height="50dp"

    <LinearLayout
        android:id="@+id/filename_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>
</ScrollView>

就像这样。另一个选项是,通过明确指定layout_height来引入外部组件,例如table layout,并将您的scrollview包含在其中。

希望它能起作用。


我不想明确指定任何东西,因为它应该根据列表中的项目数量而改变大小。无论如何,Umesh的答案正是我所需要的。 - j0ntech
它对我有效。ScrollView作为容器运作,并且我正在使用动态内容,超过20个。无论如何,很高兴你解决了它。 - Shahid V

0

如果您想要显示自定义对话框,请使用以下 XML 布局:

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ListView
        android:id="@+id/gameList"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dip" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom" >

        <Button
            android:id="@+id/load"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Load" />

        <Button
            android:id="@+id/delete"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Delete" />
    </LinearLayout>
</FrameLayout>

然后从您的类文件中这样调用它,

>     Button getList = (Button) findViewById(R.id.getList);
>     getList.setOnClickListener(new View.OnClickListener() {
>       
>             @Override
>       public void onClick(View v) {
>       // TODO Auto-generated method stub
>       final Dialog listDialog = new Dialog(MainActivity.this);
>       listDialog.setTitle("Load a game");
>       listDialog.setContentView(R.layout.custom_dialog);
>     
>       ListView gameList = (ListView) listDialog.findViewById(R.id.gameList);
>       gameList.setAdapter(new ArrayAdapter<String>          (MainActivity.this,android.R.layout.simple_list_item_1,new String[] {"your array" }));
>       
>       Button dismiss = (Button) listDialog.findViewById(R.id.load);
>       dismiss.setOnClickListener(new View.OnClickListener() {
>     
>           @Override
>           public void onClick(View v) {
>               // TODO Auto-generated method stub
>               listDialog.dismiss();
>                       }
>           });
>     
>           listDialog.show();
>       
>            } });

那个 marginBottom 的东西很聪明,但我已经得到了答案。而且,我的原始布局甚至没有 ListView。你真的读懂问题了吗? - j0ntech
你好jOntech,我已经阅读了它,并为此提供了替代解决方案。这个示例可以满足你的需求,而且是可运行的样本。你检查过它了吗? - Vishal Vaja

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