【问题标题】:Unable to stretch TableLayout to screen width无法将 TableLayout 拉伸到屏幕宽度
【发布时间】:2010-03-14 00:28:02
【问题描述】:

我已经尝试了好几个小时,但只是让 TableLayout 扩展到全屏,但没有成功。我尝试了stretchColumns、shrinkColumns,(指定列索引或使用*)你命名它。 就好像父元素(TableLayout)完全忽略了'android:layout_width =“fill_parent”'。

我在这里阅读了类似的问题,尽管他们没有为我解决问题。 下面的 XML 有 3 行,表格总是收缩到内容的大小。

显然我太新了,无法发布图片(?)这是呈现到我正在处理的项目中的 XML。 http://www.dashiva.com/images/Capture.png

我在这里错过了什么,这很令人沮丧!

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

<TableRow>
    <TextView android:id="@+id/AmountText" android:text="@string/general_amount" android:paddingRight="8dip" android:layout_gravity="center"></TextView>
    <TextView android:id="@+id/CardText" android:text="@string/general_card" android:paddingRight="8dip" android:layout_gravity="center"></TextView>
    <TextView android:text="@string/general_date" android:id="@+id/DateText" android:paddingRight="8dip" android:layout_gravity="center"></TextView>
    <TextView android:id="@+id/ErrorText" android:text="@string/general_error" android:paddingRight="8dip" android:layout_gravity="center"></TextView>
</TableRow>

<TableRow>
    <TextView android:text="Amount here..." android:id="@+id/AmountNum" android:paddingRight="8dip" android:layout_gravity="center"></TextView>
    <TextView android:id="@+id/CardNum" android:text="Num here..." android:paddingRight="8dip" android:layout_gravity="center"></TextView>
    <TextView android:text="date here..." android:id="@+id/DateDate" android:paddingRight="8dip" android:layout_gravity="center"></TextView>
    <TextView android:text="Code here..." android:id="@+id/ErrorCode" android:paddingRight="8dip" android:layout_gravity="center"></TextView>
</TableRow>

<TableRow>
    <ImageView android:id="@+id/cc_image" android:src="@drawable/mastercard" android:background="@drawable/row_bg2" android:layout_gravity="center" android:layout_weight="1"></ImageView>
    <TextView android:id="@+id/ResponseText" android:layout_span="4" android:text="Response text here..." android:textSize="18dip" android:layout_weight="1"></TextView>
</TableRow>

<ImageView android:layout_gravity="center_vertical" android:background="@drawable/gradient_bg" android:layout_height="3dip" android:layout_width="fill_parent"></ImageView>
</TableLayout>

【问题讨论】:

    标签: xml android user-interface


    【解决方案1】:

    好吧,我可以告诉你:

    1. 我删除了你的 ImageView 元素(因为我没有你的图片)并且TableLayout 很好地填充了屏幕的宽度。
    2. 您最后的&lt;TableRow&gt; 声称有五列(一列用于ImageView,四列用于TextView),而其他行有四列。

    所以,我会尝试将 android:layout_span="4" 更改为 android:layout_span="3" 看看是否有帮助。

    【讨论】:

      【解决方案2】:

      如果未设置 layout_weight,则拉伸也将不起作用。这不是你的问题,但也许它可以帮助到这里的其他人。

      (docs/guide/topics/ui/layout-objects.html 说:“子视图可以指定一个整数权重值,然后视图组中的任何剩余空间按照其声明的权重的比例分配给子视图。默认权重为零。)”

      【讨论】:

        【解决方案3】:

        好的,问题出在我以编程方式膨胀 XML 的地方。 出于某种原因,我将它膨胀到一个视图中,然后将其添加到 TableRow 中,然后将其添加到父视图中。 删除中间步骤,只需将膨胀视图添加到父元素即可解决问题。

        不过,谢谢,我的印象是跨度是一个简单的计数,而不是从零开始的。

        【讨论】:

          猜你喜欢
          • 2019-07-11
          • 2015-01-21
          • 2015-09-03
          • 1970-01-01
          • 1970-01-01
          • 2020-04-18
          • 1970-01-01
          • 2017-02-14
          • 1970-01-01
          相关资源
          最近更新 更多