2

我在活动中显示来自数据库的数据。我想要一个表格和底部的按钮。对于数据认为 TableLayout 将是它的最佳选择。我将 TableLayout 添加到 Horizo​​ntalView 和 ScrollView 使其垂直和水平滚动。我正在动态添加所有行 - 包括标题。这部分工作正常。

我想要的是当内容小于屏幕宽度时,它应该占据整个屏幕宽度。例如。如果一张桌子在纵向模式下非常适合,那么对于横向模式,它们的右侧将是空白区域。我不希望那个空间是空的,而是被所有列占据。如果行宽大于屏幕宽度,则根本没有问题 - 因为出现水平滚动条。

我尝试了一些变化,但没有任何帮助。知道要进行哪些设置以利用所有空间(如果有的话)并显示它。

是的,还有 1 个水平滚动条问题,它只出现在最后一行。我希望它出现在最后一行下方 - 所以最后一行的边框是可见的。我的 XML:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
 android:layout_height="fill_parent" android:orientation="vertical">

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:scrollbars="horizontal|vertical">

<HorizontalScrollView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="10dp" android:layout_marginBottom="10dp" > 

<TableLayout android:id="@+id/browseTable" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:layout_marginBottom="20dp"
    android:background="#FF0000" android:stretchColumns="1,2,3">

</TableLayout>

</HorizontalScrollView>
</ScrollView>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content" 
android:orientation="horizontal"  android:gravity="center" 
android:layout_marginTop="15dp">
    <Button android:id="@+id/browseAddBtn" android:text="Add" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginRight="10dp" />
    <Button android:id="@+id/browseViewBtn" android:text="Edit" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginRight="10dp"  />
    <Button android:id="@+id/browseReturnBtn" android:text="Return" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" />        
  </LinearLayout>

</LinearLayout>

输出 : 在此处输入图像描述

4

2 回答 2

5

在解决您的问题时,我发现了一件事,关于在Horizo ​​ntalScrollView中使用TableLayout。当我们在水平滚动视图中使用表格布局时, android:stretchColumns 不起作用。因此,当屏幕旋转或列的宽度数据较少时,您会得到空白。

我希望你明白这一点。我试图通过用其他一些替换水平滚动视图来解决这个问题。如果我得到解决方案,我会发布答案。再见。

编辑

Tvd终于找到了解决您问题的方法。对您的 XML 布局文件进行以下更改。

<HorizontalScrollView>

采用

        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:fillViewport="true"

如果要拉伸所有列,请stretchColumns = "*"TableLayout中使用。

再见。:-)

于 2012-02-03T09:06:52.870 回答
0

我做了一些改变。您的一些问题将通过以下布局代码解决

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

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:scrollbars="horizontal|vertical" >

    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp" >

        <TableLayout
            android:id="@+id/browseTable"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:background="#FF0000" >
        </TableLayout>
    </HorizontalScrollView>
</ScrollView>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    style="@android:style/ButtonBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/browseAddBtn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:layout_weight="1"
        android:text="Add" />

    <Button
        android:id="@+id/browseViewBtn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:layout_weight="1"
        android:text="Edit" />

    <Button
        android:id="@+id/browseReturnBtn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Return" />
</LinearLayout>

于 2012-01-23T10:55:37.497 回答