【问题标题】:Scrollview makes bottom views disappear in a DialogScrollview 使底部视图在对话框中消失
【发布时间】:2013-04-17 17:51:01
【问题描述】:

我有一个对话框,其中有一个列表(TextViews 在LinearLayout 内)在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>

看起来像这样,列表中只有几个项目:

但是当屏幕上无法容纳的内容(并且实际需要滚动)时,我的按钮会被推到屏幕下方。当一直滚动到底部时,它看起来像这样:

我需要包含按钮的LinearLayout 保留作为页脚,它不应该滚动到任何地方并且显然不会消失。我试过摆弄布局高度和重量,但无济于事。

【问题讨论】:

  • 您是否尝试将 ListView 设置为固定高度,例如 200dip?
  • 我没有实际的ListViewthere,但更改LinearLayout 的高度并没有什么不同。如果我改变ScrollView 的高度,它只会再次推出按钮。

标签: android android-layout scrollview


【解决方案1】:

尝试在scrollView中改变

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

【讨论】:

  • 这工作得很好。您能否解释一下为什么以及如何发生这种情况?
  • 这对我也有用!希望解释一下为什么会这样。
【解决方案2】:

我认为您应该明确提及 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 来引入像表格布局这样的外部组件,并按原样包含您的滚动视图。

希望有效果

【讨论】:

  • 我不想明确指定任何内容,因为它应该根据列表中的项目数改变大小。无论如何,umesh 的回答正是我所需要的。
  • 它对我有用。滚动视图用作容器,我正在使用动态内容。超过20个。无论如何,很高兴你弄清楚了
【解决方案3】:

使用下面的 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。你甚至读过这个问题吗?
  • 您好 jOntech,我已经阅读了它,我建议您使用替代解决方案。这个示例服务于您的目的,它正在运行示例。您检查了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-26
相关资源
最近更新 更多