【问题标题】:Why is each element in the table row still taking the same amount of space为什么表格行中的每个元素仍然占用相同的空间
【发布时间】:2014-09-27 07:26:09
【问题描述】:

这是我在设备上运行应用程序时得到的结果

我遇到问题的部分是行 - 带有文本、引用和网络。我在运行时将这些行动态插入到滚动视图中。这是我用于布局膨胀的 xml 代码

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent" >

    <TextView
        android:id="@+id/stockView"
        android:text="TextView"
        android:layout_weight="2" />

    <Button
        android:id="@+id/stockQuote"
        style="?android:attr/buttonStyleSmall"
        android:text="@string/get_stock_quote" 
          android:layout_weight="1" />

    <Button
        android:id="@+id/webStock"
        style="?android:attr/buttonStyleSmall"
        android:text="@string/go_to_website"
          android:layout_weight="1"  />

</TableRow>

我的问题是这段代码,为什么每个元素仍然占用相同的空间。我知道因为我将文本视图的 layout_weight 指定为 2,所以它应该占据宽度的一半,每个按钮占据总宽度的 1/4。我知道这不是权重总和的 bc,因为在这种情况下,默认情况下权重总和是 4。有谁知道我怎样才能让文本视图占据其父级宽度的 1/2?

【问题讨论】:

  • 感谢您的尝试,但没有奏效。这确实突出了所有三个具有相同宽度的事实
  • 与 weight 属性一起尝试将视图的宽度设置为 0。
  • 谢谢,但我不认为这会有所作为。至少那是我从这个线程中得到的。 stackoverflow.com/questions/26072522/…
  • “我不认为这会有所作为”???你可以解释吗?这不是官方文档关于weight 属性的说法。看看this answer 以及TableLayoutTableRowweight 所代表的内容。

标签: android xml textview tablerow


【解决方案1】:

检查它是否有效..

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent" >

<TextView
    android:id="@+id/stockView"
    android:layout_width="0dip"
    android:text="TextView"
    android:layout_weight="2" />

<Button
    android:id="@+id/stockQuote"
    android:layout_width="0dip"
    style="?android:attr/buttonStyleSmall"
    android:text="get_stock_quote"
    android:layout_weight="1" />

<Button
    android:id="@+id/webStock"
    android:layout_width="0dip"
    style="?android:attr/buttonStyleSmall"
    android:text="go_to_website"
    android:layout_weight="1"  />

【讨论】:

  • 我的原创作品。谢谢。只是预览有点偏差
  • 你能把你的问题说得更清楚吗?上面的宽度为 0dip,它可以通过这样做来工作。你甚至可以让你的体重值也像 50、25、25 一样......
【解决方案2】:

您应该使用 layout_span。尝试将 android:layout_span="2" 添加到您的文本视图中。

What is the equivalent of "colspan" in an Android TableLayout?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 2010-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    • 2017-04-13
    相关资源
    最近更新 更多