【问题标题】:Is there any solution to make this kind of table layout in android through XML?有没有办法通过XML在android中制作这种表格布局?
【发布时间】:2021-06-18 12:30:51
【问题描述】:

我正在尝试制作一个有 4 列和 7 行的表格,但第一列有 0 行。此表有任何 XML 解决方案吗?我正在附上我需要制作的桌子图片。

【问题讨论】:

    标签: xml android-studio android-layout android-tablelayout


    【解决方案1】:

    你可以尝试制作:

    1. 一个 3 列 7 行的表格

    2. 以及表格左侧的 textView

      android:layout_width="match_parent"

      android:layout_height="wrap_content"

      android:text="午餐" android:rotation="-90"/>

    【讨论】:

    • 如果重要与否,您没有角单元格,也可能更好地使用constraintlayout 作为父级,然后您可以将Textview 约束到顶部和底部表格,否则旋转的Textview 可能与表格不对齐。
    • 感谢@Andrew 添加更多解释。
    【解决方案2】:

    以下代码对我有用,并且完全符合我的要求。

        <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="4">
    
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:weightSum="1">
    
                <TableLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
    
                    <TableRow
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="1">
    
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:rotation="-90"
                            android:gravity="center"
                            android:layout_weight="1"
                            android:text="Lunch">
    
                        </TextView>
    
                    </TableRow>
                </TableLayout>
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:layout_weight="3"
                android:weightSum="3">
    
                <TableLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingLeft="10dp">
    
                    <TableRow
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
    
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:text="Diet">
    
                        </TextView>
    
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:text="Quantity">
    
                        </TextView>
    
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:text="Image">
    
                        </TextView>
    
                    </TableRow>
    
                    <TableRow
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
    
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:text="Diet">
    
                        </TextView>
    
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:text="Quantity">
    
                        </TextView>
    
                        <ImageView
                            android:layout_width="20dp"
                            android:layout_height="20dp"
                            android:layout_weight="1">
    
                        </ImageView>
    
                    </TableRow>
    
                    <TableRow
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
    
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:text="Diet">
    
                        </TextView>
    
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:text="Quantity">
    
                        </TextView>
    
                        <ImageView
                            android:layout_width="20dp"
                            android:layout_height="20dp"
                            android:layout_weight="1">
    
                        </ImageView>
    
                    </TableRow>
    
                    <TableRow
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
    
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:text="Diet">
    
                        </TextView>
    
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:text="Quantity">
    
                        </TextView>
    
                        <ImageView
                            android:layout_width="20dp"
                            android:layout_height="20dp"
                            android:layout_weight="1">
    
                        </ImageView>
    
                    </TableRow>
    
                    <TableRow
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
    
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:text="Diet">
    
                        </TextView>
    
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:text="Quantity">
    
                        </TextView>
    
                        <ImageView
                            android:layout_width="20dp"
                            android:layout_height="20dp"
                            android:layout_weight="1">
    
                        </ImageView>
    
                    </TableRow>
    
                    <TableRow
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
    
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:text="Diet">
    
                        </TextView>
    
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:text="Quantity">
    
                        </TextView>
    
                        <ImageView
                            android:layout_width="20dp"
                            android:layout_height="20dp"
                            android:layout_weight="1">
    
                        </ImageView>
    
                    </TableRow>
    
                    <TableRow
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
    
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:text="Diet">
    
                        </TextView>
    
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:text="Quantity">
    
                        </TextView>
    
                        <ImageView
                            android:layout_width="20dp"
                            android:layout_height="20dp"
                            android:layout_weight="1">
    
                        </ImageView>
    
                    </TableRow>
                </TableLayout>
    
    
            </LinearLayout>
    
    
        </LinearLayout>
    
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多