【问题标题】:Layout problem: scrollview inside a table, inside a custom dialog布局问题:表格内的滚动视图,自定义对话框内
【发布时间】:2010-05-25 05:22:03
【问题描述】:

我有一个无法解决的布局问题。我在这里浏览了很多帖子,我的帖子似乎很独特,可以发布一个问题。

我创建了一个自定义对话框,它以编程方式创建一个 3 行表。顶行和底行有文本,而中间行包含一个 ScrollView,其中包含一个 LinearLayout。然后,LinearLayout 包含一些视图(本例中为 TextView)。由于我以编程方式执行此操作,请参阅下面的 XML 伪代码,以及下面的实际代码。

我想要发生的是,当 LinearLayout 中包含的内容的高度变得太大时,ScrollView 会完成它的工作,并且页眉和页脚 TextView 始终可见。

问题是当对话框变得足够大时,ScrollView 似乎接管了整个对话框,而我的页脚 TextView 从屏幕上消失了。 我该怎么做才能确保页脚 TextView 永远不会消失,但 ScrollView 仍然可以运行?

查看以下图片链接:

页脚在左侧可见,在右侧消失:

http://img690.imageshack.us/i/screenshotss.png/

<TableLayout>
    <TableRow>
        <TextView>
    </TableRow>
    <TableRow>
        <ScrollView>
            <LinearLayout>
                <TextView/>
                <TextView/>
                <TextView/>
                ...
            </LinearLayout>
        </ScrollView>
    </TableRow>
    <TableRow>
        <TextView>
    </TableRow>
</TableLayout>

代码如下:

public class DialogScrollTest extends Dialog {

    public DialogScrollTest(Context ctx){
        super(ctx);

        setTitle("");
        requestWindowFeature(Window.FEATURE_NO_TITLE); 

        TableLayout table = new TableLayout(ctx);       
        TableRow row;       
        TextView text;

        row = new TableRow(ctx);
        {
            text = new TextView(ctx);
            text.setText("TestTop");
            row.addView(text);
        }
        table.addView(row);

        row = new TableRow(ctx);
        {
            ScrollView scroll = new ScrollView(ctx);
            {   
                LinearLayout linear = new LinearLayout(ctx);
                linear.setOrientation(LinearLayout.VERTICAL);
                for(int t=0; t<32; ++t){
                    text = new TextView(ctx);
                    text.setText("TestScroll");
                    linear.addView(text);                   
                }
                scroll.addView(linear);
            }       
            row.addView(scroll);
        }
        table.addView(row);

        row = new TableRow(ctx);
        {
            text = new TextView(ctx);
            text.setText("TestBottom");
            row.addView(text);
        }
        table.addView(row);

        this.setContentView(table);
    }   
}

【问题讨论】:

    标签: android android-widget


    【解决方案1】:

    您可以只使用带有页脚和页眉的 ListView 吗?或者它是一个示例数据,实际上您正在尝试做一些更复杂的事情?

    【讨论】:

    • ScrollView 中的 TextView 对象只是示例——我想要使用的实际界面是相当随意的。因此,它并不适合 ListView 和适配器。我希望有某种方法可以与中间的 TableRow 进行通信,即它的最大大小受到限制,因为不强制底部的 TableRow 离开对话框,然后 ScrollView 将不得不解决剩余的空间。有谁知道设置此约束的正确方法?
    • 另外,当 ScrollView 包含多页内容时,我可以使用 RelativeLayout 来完成这项工作。但是,当该内容只有几行时,ScrollView 不会缩小以包装内容。尽管在其上设置了 WRAP。我猜RelativeLayout 优先。这令人沮丧。
    • 我仍然觉得 ListView 可能是一个不错的选择,如果您有使用相同布局的项目(即使它是一个复杂的布局)。但是如果不亲自尝试,我就无法回答 tablerow 问题。对不起...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    相关资源
    最近更新 更多