【问题标题】:android XML layout not working as expected with tablelayout and scrollviewandroid XML 布局在 tablelayout 和 scrollview 中无法正常工作
【发布时间】:2011-04-22 15:32:57
【问题描述】:

编辑: TableLayout 不能正常工作吗?我看到你们中的大多数人都说使用相对布局,而跳过 tableview?

我试图摆脱数据 1 和数据 2 之后的空间。如果我在没有 TableLayout 的情况下放置相同的 XML 代码,那么它看起来没问题。我要改变什么?

带有“100”的第三行我想要右侧的 2 个条目文本视图。我已经尝试了我能想到的一切,但它不会向右走。我删除了第二个 textview 以查看是否有帮助,但没有太大区别。

谁能告诉我我做错了什么?谢谢!

这是 XML:

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

<ScrollView android:layout_width="fill_parent"
    android:layout_height="wrap_content">


    <TableLayout android:layout_width="fill_parent"
        android:stretchColumns="*" android:layout_height="fill_parent"
        android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"
        android:padding="6px">

        <TableRow style="@style/FormRows" android:layout_marginTop="15sp">
            <Button android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:layout_marginLeft="5px"
                android:layout_span="3" android:layout_weight="1" android:text="Save"
                android:id="@+id/dgSave" />
        </TableRow>



        <TableRow style="@style/FormRows">

            <TextView android:layout_width="70sp"
                android:layout_height="wrap_content" android:textSize="16sp"
                android:layout_weight="1" android:text="Data 1" />

            <Spinner android:id="@+id/dgData0" android:layout_weight="2"
                android:layout_width="wrap_content" android:layout_height="wrap_content" />

        </TableRow>



        <TableRow>

            <LinearLayout android:orientation="horizontal"
                android:layout_width="fill_parent" android:layout_height="wrap_content">

        <EditText android:text="100" android:selectAllOnFocus="true"
                    android:imeOptions="actionNext" android:numeric="integer"
                    android:width="100sp" android:id="@+id/dgWarn0" android:gravity="right"
                    android:layout_height="wrap_content" android:layout_weight="1"
                    android:layout_gravity="right" android:layout_span="2" />


                <EditText android:text="100" android:selectAllOnFocus="true"
                    android:imeOptions="actionNext" android:numeric="integer"
                    android:width="100sp" android:id="@+id/dgWarn1" android:gravity="right"
                    android:layout_height="wrap_content" android:layout_weight="1"
                    android:layout_gravity="right" android:layout_span="2" />

            </LinearLayout>

        </TableRow>

        <TableRow style="@style/FormRows">

            <TextView android:layout_width="70sp"
                android:layout_height="wrap_content" android:textSize="16sp"
                android:layout_weight="1" android:text="Data 2" />

            <Spinner android:id="@+id/dgData1" android:layout_weight="2"
                android:layout_width="wrap_content" android:layout_height="wrap_content" />

        </TableRow>


    </TableLayout>
</ScrollView>

谢谢!寻求帮助!!! 标记

【问题讨论】:

  • 你应该使用表格布局吗?因为我们可以使用相对布局来实现这个布局。
  • 我认为Data后面的空格是因为android:layout_weight="1",但你说你可以接受这个xml w/o TableLayout...
  • 我不知道为什么,可能是因为我刚刚发现自己学习了 LinearLayout 和 RelativeLayout 并一遍又一遍地重复使用它们,但我从来没有使用过 Table Layout...从来没有用过为了它。使用相对布局将非常容易。将 Save 按钮放在您想要的位置,将 Spinners 右边缘与保存按钮的右边缘对齐,将 Data 标签放在 Spinners 的右侧,等等......应该很容易。
  • 您应该查看 Google 的教程,看看他们如何使用 TableLayout:developer.android.com/guide/tutorials/views/…
  • 你能分享一个你想要的布局的模型吗? @Venkatesh 提到使用 RelativeLayout 解决问题。我完全同意这一点,对于这种复杂的视图来说,它要方便得多。 TableLayout 是为表格 (...) 创建的,使用 RelativeLayout 您还可以节省布局数量,因此它将是您的最佳选择。

标签: android xml scrollview tablelayout


【解决方案1】:

RelativeLayout 可能如下所示。我很快就做到了……它缺少其他一些边距等……但这是一个起点。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <Button android:id="@+id/save"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_margin="20dp"
        android:text="Save" />
    <Spinner android:id="@+id/spinner1"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignRight="@id/save" />
    <TextView
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_toLeftOf="@id/spinner1"
        android:text="Data 1" />
    <EditText android:id="@+id/editbox"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignRight="@id/save"
        android:layout_below="@id/spinner1" />
    <Spinner android:id="@+id/spinner2"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignRight="@id/save"
        android:layout_below="@id/editbox" />
    <TextView
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_toLeftOf="@id/spinner2"
        android:text="Data 2" />
</RelativeLayout>

【讨论】:

  • 与相对布局相比,TableLayout 是否高效?
  • 我不知道任何效率差异......但只涉及少数视图,我无法想象它会产生很大的不同。
【解决方案2】:

嘿, 我快速浏览了一下,也许 TableLayout 现在是这个特定活动的最佳选择。 你有没有考虑过使用RelativeLayout?在那里,您可以通过将它们放置在其他小部件附近来定义小部件的位置(例如某些按钮位于文本视图下)。

这只是一个建议,也许还有比这个具体问题更多的问题,我还没有了解完整的情况。

您可以在此处阅读有关 RealtiveLayout 的信息:

http://developer.android.com/reference/android/widget/RelativeLayout.html

http://developer.android.com/resources/tutorials/views/hello-relativelayout.html

edit:Maximus,伟大的思想都一样,我们之间的 30 秒 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-24
    • 1970-01-01
    • 2021-06-02
    • 1970-01-01
    • 2014-07-13
    • 2020-05-17
    • 2022-11-04
    • 2012-10-10
    相关资源
    最近更新 更多