java - 使 TableRows 宽度均匀

标签 java android width tablelayout tablerow

下面是我的屏幕:

enter image description here

您可以看到,在第 1 行中,两个按钮没有均匀对齐,因为右侧框为 2 行,左侧框为 1 行。另外,您可以在第 3 行中看到按钮更宽,因为它们都是 3 行。

有没有办法让行的高度相同?有点像在 LinearLayout 中如何使用 android:layout_width="#"。我已将所有代码发布在 XML 中,因为它相对较短。

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relativelayout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="1dp"
    android:paddingBottom="50dp"
    android:background="@drawable/scroll" >

    <TextView
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="14sp"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:paddingTop="65dp" />

    <TextView
        android:id="@+id/subHeader"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/header"
        android:textStyle="italic|bold"
        android:textSize="12sp"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:paddingBottom="20dp" />

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/subHeader"
        android:layout_alignParentBottom="true"
        android:background="@drawable/scrollviewborder"
        android:fillViewport="true"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:paddingTop="8dp"
        android:paddingBottom="8dp" >

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <Button
                android:id="@+id/jc" 
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:textStyle="bold"
                android:textSize="14sp" />

            <Button 
                android:id="@+id/tencommandments"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:textStyle="bold"
                android:textSize="14sp" />
        </TableRow>

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <Button
                android:id="@+id/exodus" 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textStyle="bold"
                android:textSize="14sp" />

            <Button
                android:id="@+id/genesis" 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textStyle="bold"
                android:textSize="14sp" />
        </TableRow>

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <Button
                android:id="@+id/holydays" 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textStyle="bold"
                android:textSize="14sp" />      

            <Button
                android:id="@+id/facts" 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textStyle="bold"
                android:textSize="14sp" />
        </TableRow>

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <Button
                android:id="@+id/random" 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textStyle="bold"
                android:textSize="14sp" />
        </TableRow>
    </TableLayout>
</RelativeLayout>

最佳答案

这是您的布局,已重做。您将需要添加引用您的可绘制对象的行——我将它们取出,因为我无权访问它们。请注意权重值的小数形式,并以该空间的百分比来考虑容器内小部件的可用空间量。这种范式转变应该会提升您对权重属性的理解。

此外,请尽量避免使用 fill_parent,它已在 API 8 及更高版本中被弃用,取而代之的是 match_parent,除非您确实想支持旧的、发霉的 Android 版本(不到安装基数的 1%)。

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relativelayout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="1dp"
    android:paddingBottom="50dp" >

    <TextView
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="14sp"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:paddingTop="65dp" />

    <TextView
        android:id="@+id/subHeader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/header"
        android:textStyle="italic|bold"
        android:textSize="12sp"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:paddingBottom="20dp" />

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/subHeader"
        android:layout_alignParentBottom="true"
        android:fillViewport="true"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:paddingTop="8dp"
        android:paddingBottom="8dp" >

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight=".25" >

            <Button
                android:id="@+id/jc" 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight=".5"
                android:textStyle="bold"
                android:textSize="14sp" />

            <Button 
                android:id="@+id/tencommandments"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight=".5"
                android:textStyle="bold"
                android:textSize="14sp" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight=".25" >

            <Button
                android:id="@+id/exodus" 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight=".5"
                android:textStyle="bold"
                android:textSize="14sp" />

            <Button
                android:id="@+id/genesis" 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight=".5"
                android:textStyle="bold"
                android:textSize="14sp" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight=".25" >

            <Button
                android:id="@+id/holydays" 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight=".5"
                android:textStyle="bold"
                android:textSize="14sp" />      

            <Button
                android:id="@+id/facts" 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight=".5"
                android:textStyle="bold"
                android:textSize="14sp" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight=".25" >

            <Button
                android:id="@+id/random" 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:textStyle="bold"
                android:textSize="14sp" />
        </TableRow>
    </TableLayout>
</RelativeLayout>

关于java - 使 TableRows 宽度均匀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16148694/

相关文章:

java - 我可以在不使用该点运算符的情况下调用方法吗

Java 计算器帮助

android - 开发 Android 智能手机应用程序 - 您建议在哪些设备上检查该应用程序?

html - 使用 CSS 和 HTML 将父 Div 扩展到元素的宽度

jquery - 将图像放置在浏览器的 100% 宽度和 100% 高度的 div 中

html - 使外部 block 与内部行内 block 的宽度一样宽

java - HTTP 状态 500 - ... IllegalArgumentException : Unknown return value type when I use ResponseStatus annotation and return value in spring controller

java - 有人可以向我解释这个方法是如何工作的吗?

android - 自定义操作栏显示将项目升级到 Android SDK 版本 21 后留下的深色填充

android - 开始 Activity