【问题标题】:Android : Unable to set TableLayout properly as wantedAndroid:无法根据需要正确设置 TableLayout
【发布时间】:2012-02-16 15:22:52
【问题描述】:

我在 Activity 中显示来自 DB 的数据。我想要一个表格和底部的按钮。对于数据认为 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>

输出:

【问题讨论】:

  • 捕捉这张图片,并添加到这里,然后告诉你到底想要什么。它会帮助其他人帮助你。 :) 谢谢
  • @AnkitA,添加了一张图片来展示我拥有的东西并解释我想要的东西。希望它会帮助其他人帮助我。
  • 尝试我的回答不是全部,但是是的,您的一些问题将由此解决。第二,如果你想显示浮点值。尝试展示到一定长度。小数点后4位..
  • @AnkitA, REG 显示浮点值:我没明白你的意思我的意思是怎么做?我只是获取值,将其转换为 String() 并在 TextView 中设置数据: obj.getValue(field).toString()
  • 你可以像 obj.getValue(field).toString().substring(0,7) 那样做,或者你可以得到 '.' 的索引。并且可以说 substring.(0,str.indexof(",")+4) 像 dat

标签: android width scrollbar android-tablelayout


【解决方案1】:

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

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

编辑

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

&lt;HorizontalScrollView&gt;

使用

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

如果您希望所有列都被拉伸,请在 TableLayout 中使用 stretchColumns = "*"

再见。 :-)

【讨论】:

  • 哦@Yugandhar Babu,哇,太好了。 fillViewPort 完成了这项工作。之前也问过这个Q,但没有正确的回答。你的小费帮助了我。我非常感谢您的帮助热线。非常感谢。你不知道这在我的待办事项列表中,因为我猜想 20 多天。尝试了几件事,但没有得到想要的结果。再次感谢。
  • 很好的答案!
【解决方案2】:

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

 <?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>

【讨论】:

  • 我在 ScrollView 中添加了更改 layout_weight,但这对我没有帮助。它没有导致输出发生任何变化。
  • 我喜欢你的“ButtonBar”风格创意。我也想把它放在底部,但没有办法做到这一点。你的风格想法成功了。感谢 ButtonBar 风格的想法。
  • 尝试为水平滚动视图填充父级,让我帮助你。
  • 这也没有任何好处。线性、滚动、水平和表格的这个 layout_width 是“fill_parent”。 TableRow 的 TableLayout.LayoutParams 也是宽度的“Fill_parent”。 TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
  • 我也尝试将stretchcolumns="1, 2, 3" 添加到tableLayout,但这也没有用。 android:stretchColumns="1, 2, 3"
猜你喜欢
  • 1970-01-01
  • 2020-12-18
  • 1970-01-01
  • 2017-01-07
  • 1970-01-01
  • 2014-06-15
  • 2012-06-05
  • 2012-05-19
  • 1970-01-01
相关资源
最近更新 更多