android:如何强制TableRows具有相同的高度?

标签 android layout height tablelayout tablerow

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

现在,TableLayout 中有八行,每行包含 5 个 LinearLayouts。每个 LinearLayout 包含 2 个 TextViewFontSizesTextViewTextView 不同。

事实证明,TableRow 的高度有所不同,具体取决于所包含的 TextView 的字体大小。 奇怪的是,无论我将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>

最佳答案

根据Android developer Documentation of TableLayout ,你不能这样做。

The children of a TableLayout cannot specify the layout_width attribute. Width is always MATCH_PARENT. However, the layout_height attribute can be defined by a child; default value is ViewGroup.LayoutParams.WRAP_CONTENT. If the child is a TableRow, then the height is always ViewGroup.LayoutParams.WRAP_CONTENT.

但是,您可以使用垂直 LinearLayout 来实现类似的效果。

关于android:如何强制TableRows具有相同的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15137674/

相关文章:

android - FFmpeg:如何转换背景模糊的垂直视频

android - 谷歌播放警告 : How to fix incorrect implementation of HostnameVerifier?

java - 向组件 Vaadin 添加属性

css - Div 高度 100% 导致溢出问题

html - CSS 高度百分比从下到上填充

java - 如何在一行中保存字符串?

java - exoplayer 2.11.* 版本在 android 5.0/5.1(Lollipop) 设备中引发渲染器错误

android - R.java 是否强制我所有的布局元素成为基于其 ID 的公共(public)变量?

html - 如何在Grails中创建自定义 View 和布局

jquery - 响应 slider 图像高度自动不接受 Chrome