【问题标题】:android: How to force TableRows to have the same height?android:如何强制 TableRows 具有相同的高度?
【发布时间】:2013-02-14 18:29:05
【问题描述】:

我已经彻底搜索了 google 和 stackoverflow,但我得到的唯一答案是:将每个 Tablerow 设置为 layout weight = "1"。这不起作用。

现在我在TableLayout 中有八行,每行包含 5 个LinearLayouts。每个 LinearLayouts 包含 2 个TextViewsFontSizesTextView 变化到TextView

事实证明,TableRows 的高度不同,具体取决于包含的TextViews 的字体大小。 奇怪的是,我将 TableRows 的 layout_height 设置为“0dp”或“match_parent”或“fill_parent”对布局没有任何影响。

如何强制TableRows 具有相同的高度?我不想使用特定的 pts 值作为高度,因为 tableRows 必须在它们之间平均划分整个屏幕。

这里是xml的一部分,实际文件很大:

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="6"
    android:weightSum="8"
    tools:context=".MainActivity" >

    <TableRow
        android:layout_width="match_parent"
        android:layout_weight="1"
        tools:context=".MainActivity">

        <LinearLayout
            android:clickable="true"
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginTop="@dimen/btnmargintop"
            android:layout_marginBottom="@dimen/btnmarginbot"
            android:layout_marginLeft="@dimen/btnmarginleft"
            android:layout_marginRight="@dimen/btnmarginright"
            android:layout_weight="1"
            android:background="@drawable/bg" >

            <myapp.TypefacedTextView
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="@integer/layoutweight1"
                android:layout_gravity="center"
                stevens:typeface="myfont.otf"
                android:clickable="false"
                android:textSize="@dimen/fontsize1" />
            <myapp.TypefacedTextView
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="@integer/layoutweight2"
                android:layout_gravity="center"
                stevens:typeface="myfont.otf"
                android:clickable="false"
                android:textSize="@dimen/fontsize2" />              
        </LinearLayout>

【问题讨论】:

  • 请发布您的代码和您的 XML。
  • 你能做点什么吗,菲尔?

标签: android layout height tablelayout tablerow


【解决方案1】:

您可能正在让标签的布局自动调整为内部内容的大小。有两种方法可以解决问题:

  • 确保所有元素的大小相同 - 话虽如此,不要担心整个表格,只需确保内部元素的尺寸相同
  • 或者简单地设置父布局的高度,不要让元素产生影响。这个解决方案的问题是,如果你的内部变大,你就会没有空间,所以你可能要坚持第一个选项

【讨论】:

  • 我不太确定,但我认为选项 1 的意思是我将每行的高度设置为固定的 pts 值?我不希望那样,因为那样的话,根据屏幕大小,表格不会填满屏幕..
  • 是的,完全正确!使所有元素的大小相同(=找到它可以具有的最大大小),这样,所有行的高度都相同。为什么你需要把它放在屏幕上,它会调整+用户可以向下滚动;)
  • 谢谢你的帮助,但我需要它来适应屏幕。滚动不是一种选择。
  • 在这种情况下,选项二是方法,你需要去。
  • 将每行的weight设置为相同,并设置高度fill_parent。这样,您将拥有相同大小的所有行,并且无需滚动即可适应屏幕。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-09
  • 2020-02-14
  • 2023-03-09
  • 2017-03-31
  • 1970-01-01
  • 2021-08-31
相关资源
最近更新 更多