我想创建一个包含6行3列的基本表。行代表天,列代表早晨/晚上。我希望创建的布局:
我希望能够为节点着色(红色和绿色以确保可用性)。此外,每行都固定在屏幕的整个宽度上。我用TableLayout
创建了类似的东西:
<TableLayout
android:id="@+id/shifts_table"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:shrinkColumns="*"
android:stretchColumns="*"
android:layout_marginBottom="10dp">
<TableRow
android:id="@+id/table_row_sunday"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text_sunday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/form_sunday"/>
<LinearLayout
android:id="@+id/layout_sunday_morning"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/checkbox_sunday_morning"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:clickable="false"
android:layout_width="wrap_content" />
<TextView
android:id="@+id/text_sunday_morning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/form_morning"/>
</LinearLayout>
<LinearLayout
android:id="@+id/layout_sunday_evening"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/checkbox_sunday_evening"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:clickable="false"
android:layout_width="wrap_content" />
<TextView
android:id="@+id/text_sunday_evening"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/form_evening"/>
</LinearLayout>
</TableRow>
<TableRow
android:id="@+id/table_row_monday"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:id="@+id/text_monday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/form_monday" />
<LinearLayout
android:id="@+id/layout_monday_morning"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/checkbox_monday_morning"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:clickable="false"
android:layout_width="wrap_content" />
<TextView
android:id="@+id/text_monday_morning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/form_morning"/>
</LinearLayout>
<LinearLayout
android:id="@+id/layout_monday_evening"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/checkbox_monday_evening"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:clickable="false"
android:layout_width="wrap_content" />
<TextView
android:id="@+id/text_monday_evening"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/form_evening"/>
</LinearLayout>
</TableRow>
<TableRow
android:id="@+id/table_row_tuesday"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:id="@+id/text_tuesday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/form_tuesday"/>
<LinearLayout
android:id="@+id/layout_tuesday_morning"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/checkbox_tuesday_morning"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:clickable="false"
android:layout_width="wrap_content" />
<TextView
android:id="@+id/text_tuesday_morning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/form_morning"/>
</LinearLayout>
<LinearLayout
android:id="@+id/layout_tuesday_evening"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/checkbox_tuesday_evening"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:clickable="false"
android:layout_width="wrap_content" />
<TextView
android:id="@+id/text_tuesday_evening"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/form_evening"/>
</LinearLayout>
</TableRow>
<TableRow
android:id="@+id/table_row_wednesday"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:id="@+id/text_wednesday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/form_wednesday"/>
<LinearLayout
android:id="@+id/layout_wednesday_morning"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/checkbox_wednesday_morning"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:clickable="false"
android:layout_width="wrap_content" />
<TextView
android:id="@+id/text_wednesday_morning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/form_morning"/>
</LinearLayout>
<LinearLayout
android:id="@+id/layout_wednesday_evening"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/checkbox_wednesday_evening"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:clickable="false"
android:layout_width="wrap_content" />
<TextView
android:id="@+id/text_wednesday_evening"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/form_evening" />
</LinearLayout>
</TableRow>
<TableRow
android:id="@+id/table_row_thursday"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:id="@+id/text_thursday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/form_thursday"/>
<LinearLayout
android:id="@+id/layout_thursday_morning"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/checkbox_thursday_morning"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:clickable="false"
android:layout_width="wrap_content" />
<TextView
android:id="@+id/text_thursday_morning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/form_morning"/>
</LinearLayout>
<LinearLayout
android:id="@+id/layout_thursday_evening"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/checkbox_thursday_evening"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:clickable="false"
android:layout_width="wrap_content" />
<TextView
android:id="@+id/text_thursday_evening"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/form_evening" />
</LinearLayout>
</TableRow>
</TableLayout>
我有一个复选框(而不是要填充的节点)。我尝试将TableLayout
转换为GridLayout
(不带复选框),但看起来并不是一件简单的任务。从这个topic中,我知道要对行加淀粉并不容易。用Grid
和API 16创建此类表的最简单方法是什么?
答案 0 :(得分:0)
代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.gridlayout.widget.GridLayout
android:id="@+id/grid_layout_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:background="@android:color/black"
android:padding="1dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/item_1"
android:layout_width="wrap_content"
android:layout_height="@dimen/grid_item_height"
android:layout_marginBottom="@dimen/grid_item_margin"
android:layout_marginEnd="@dimen/grid_item_margin"
android:layout_marginLeft="@dimen/grid_item_margin"
android:layout_marginRight="@dimen/grid_item_margin"
android:layout_marginStart="@dimen/grid_item_margin"
android:layout_marginTop="@dimen/grid_item_margin"
android:background="@color/grid_items_background_color"
android:text=""
android:textAllCaps="false"
android:textColor="@android:color/black"
android:textSize="@dimen/grid_text_size"
android:gravity="center"
app:layout_column="0"
app:layout_columnWeight="1"
app:layout_gravity="fill"
app:layout_row="0"
app:layout_rowWeight="1" />
<TextView
android:id="@+id/item_2"
android:layout_width="wrap_content"
android:layout_height="@dimen/grid_item_height"
android:layout_marginBottom="@dimen/grid_item_margin"
android:layout_marginEnd="@dimen/grid_item_margin"
android:layout_marginLeft="@dimen/grid_item_margin"
android:layout_marginRight="@dimen/grid_item_margin"
android:layout_marginStart="@dimen/grid_item_margin"
android:layout_marginTop="@dimen/grid_item_margin"
android:background="@color/grid_items_background_color"
android:text="@string/morning"
android:textAllCaps="false"
android:textColor="@android:color/black"
android:textSize="@dimen/grid_text_size"
android:gravity="center"
app:layout_column="1"
app:layout_columnWeight="1"
app:layout_gravity="fill"
app:layout_row="0"
app:layout_rowWeight="1" />
<TextView
android:id="@+id/item_3"
android:layout_width="wrap_content"
android:layout_height="@dimen/grid_item_height"
android:layout_marginBottom="@dimen/grid_item_margin"
android:layout_marginEnd="@dimen/grid_item_margin"
android:layout_marginLeft="@dimen/grid_item_margin"
android:layout_marginRight="@dimen/grid_item_margin"
android:layout_marginStart="@dimen/grid_item_margin"
android:layout_marginTop="@dimen/grid_item_margin"
android:background="@color/grid_items_background_color"
android:text="@string/evening"
android:textAllCaps="false"
android:textColor="@android:color/black"
android:textSize="@dimen/grid_text_size"
android:gravity="center"
app:layout_column="2"
app:layout_columnWeight="1"
app:layout_gravity="fill"
app:layout_row="0"
app:layout_rowWeight="1" />
<TextView
android:id="@+id/item_4"
android:layout_width="wrap_content"
android:layout_height="@dimen/grid_item_height"
android:layout_marginBottom="@dimen/grid_item_margin"
android:layout_marginEnd="@dimen/grid_item_margin"
android:layout_marginLeft="@dimen/grid_item_margin"
android:layout_marginRight="@dimen/grid_item_margin"
android:layout_marginStart="@dimen/grid_item_margin"
android:layout_marginTop="@dimen/grid_item_margin"
android:background="@color/grid_items_background_color"
android:text="@string/monday"
android:textAllCaps="false"
android:textColor="@android:color/black"
android:textSize="@dimen/grid_text_size"
android:gravity="center"
app:layout_column="0"
app:layout_columnWeight="1"
app:layout_gravity="fill"
app:layout_row="1"
app:layout_rowWeight="1" />
<TextView
android:id="@+id/item_5"
android:layout_width="wrap_content"
android:layout_height="@dimen/grid_item_height"
android:layout_marginBottom="@dimen/grid_item_margin"
android:layout_marginEnd="@dimen/grid_item_margin"
android:layout_marginLeft="@dimen/grid_item_margin"
android:layout_marginRight="@dimen/grid_item_margin"
android:layout_marginStart="@dimen/grid_item_margin"
android:layout_marginTop="@dimen/grid_item_margin"
android:background="@color/grid_items_background_color"
android:text=""
android:textAllCaps="false"
android:textColor="@android:color/black"
android:textSize="@dimen/grid_text_size"
android:gravity="center"
app:layout_column="1"
app:layout_columnWeight="1"
app:layout_gravity="fill"
app:layout_row="1"
app:layout_rowWeight="1" />
<TextView
android:id="@+id/item_6"
android:layout_width="wrap_content"
android:layout_height="@dimen/grid_item_height"
android:layout_marginBottom="@dimen/grid_item_margin"
android:layout_marginEnd="@dimen/grid_item_margin"
android:layout_marginLeft="@dimen/grid_item_margin"
android:layout_marginRight="@dimen/grid_item_margin"
android:layout_marginStart="@dimen/grid_item_margin"
android:layout_marginTop="@dimen/grid_item_margin"
android:background="@android:color/holo_red_light"
android:text=""
android:textAllCaps="false"
android:textColor="@android:color/black"
android:textSize="@dimen/grid_text_size"
android:gravity="center"
app:layout_column="2"
app:layout_columnWeight="1"
app:layout_gravity="fill"
app:layout_row="1"
app:layout_rowWeight="1" />
<TextView
android:id="@+id/item_7"
android:layout_width="wrap_content"
android:layout_height="@dimen/grid_item_height"
android:layout_marginBottom="@dimen/grid_item_margin"
android:layout_marginEnd="@dimen/grid_item_margin"
android:layout_marginLeft="@dimen/grid_item_margin"
android:layout_marginRight="@dimen/grid_item_margin"
android:layout_marginStart="@dimen/grid_item_margin"
android:layout_marginTop="@dimen/grid_item_margin"
android:background="@color/grid_items_background_color"
android:text="@string/tuesday"
android:textAllCaps="false"
android:textColor="@android:color/black"
android:textSize="@dimen/grid_text_size"
android:gravity="center"
app:layout_column="0"
app:layout_columnWeight="1"
app:layout_gravity="fill"
app:layout_row="2"
app:layout_rowWeight="1" />
<TextView
android:id="@+id/item_8"
android:layout_width="wrap_content"
android:layout_height="@dimen/grid_item_height"
android:layout_marginBottom="@dimen/grid_item_margin"
android:layout_marginEnd="@dimen/grid_item_margin"
android:layout_marginLeft="@dimen/grid_item_margin"
android:layout_marginRight="@dimen/grid_item_margin"
android:layout_marginStart="@dimen/grid_item_margin"
android:layout_marginTop="@dimen/grid_item_margin"
android:background="@android:color/holo_red_light"
android:text=""
android:textAllCaps="false"
android:textColor="@android:color/black"
android:textSize="@dimen/grid_text_size"
android:gravity="center"
app:layout_column="1"
app:layout_columnWeight="1"
app:layout_gravity="fill"
app:layout_row="2"
app:layout_rowWeight="1" />
<TextView
android:id="@+id/item_9"
android:layout_width="wrap_content"
android:layout_height="@dimen/grid_item_height"
android:layout_marginBottom="@dimen/grid_item_margin"
android:layout_marginEnd="@dimen/grid_item_margin"
android:layout_marginLeft="@dimen/grid_item_margin"
android:layout_marginRight="@dimen/grid_item_margin"
android:layout_marginStart="@dimen/grid_item_margin"
android:layout_marginTop="@dimen/grid_item_margin"
android:background="@color/grid_items_background_color"
android:text=""
android:textAllCaps="false"
android:textColor="@android:color/black"
android:textSize="@dimen/grid_text_size"
android:gravity="center"
app:layout_column="2"
app:layout_columnWeight="1"
app:layout_gravity="fill"
app:layout_row="2"
app:layout_rowWeight="1" />
<TextView
android:id="@+id/item_10"
android:layout_width="wrap_content"
android:layout_height="@dimen/grid_item_height"
android:layout_marginBottom="@dimen/grid_item_margin"
android:layout_marginEnd="@dimen/grid_item_margin"
android:layout_marginLeft="@dimen/grid_item_margin"
android:layout_marginRight="@dimen/grid_item_margin"
android:layout_marginStart="@dimen/grid_item_margin"
android:layout_marginTop="@dimen/grid_item_margin"
android:background="@color/grid_items_background_color"
android:text="@string/wednesday"
android:textAllCaps="false"
android:textColor="@android:color/black"
android:textSize="@dimen/grid_text_size"
android:gravity="center"
app:layout_column="0"
app:layout_columnWeight="1"
app:layout_gravity="fill"
app:layout_row="3"
app:layout_rowWeight="1" />
<TextView
android:id="@+id/item_11"
android:layout_width="wrap_content"
android:layout_height="@dimen/grid_item_height"
android:layout_marginBottom="@dimen/grid_item_margin"
android:layout_marginEnd="@dimen/grid_item_margin"
android:layout_marginLeft="@dimen/grid_item_margin"
android:layout_marginRight="@dimen/grid_item_margin"
android:layout_marginStart="@dimen/grid_item_margin"
android:layout_marginTop="@dimen/grid_item_margin"
android:background="@android:color/holo_red_light"
android:text=""
android:textAllCaps="false"
android:textColor="@android:color/black"
android:textSize="@dimen/grid_text_size"
android:gravity="center"
app:layout_column="1"
app:layout_columnWeight="1"
app:layout_gravity="fill"
app:layout_row="3"
app:layout_rowWeight="1" />
<TextView
android:id="@+id/item_12"
android:layout_width="wrap_content"
android:layout_height="@dimen/grid_item_height"
android:layout_marginBottom="@dimen/grid_item_margin"
android:layout_marginEnd="@dimen/grid_item_margin"
android:layout_marginLeft="@dimen/grid_item_margin"
android:layout_marginRight="@dimen/grid_item_margin"
android:layout_marginStart="@dimen/grid_item_margin"
android:layout_marginTop="@dimen/grid_item_margin"
android:background="@android:color/holo_red_light"
android:text=""
android:textAllCaps="false"
android:textColor="@android:color/black"
android:textSize="@dimen/grid_text_size"
android:gravity="center"
app:layout_column="2"
app:layout_columnWeight="1"
app:layout_gravity="fill"
app:layout_row="3"
app:layout_rowWeight="1" />
<TextView
android:id="@+id/item_13"
android:layout_width="wrap_content"
android:layout_height="@dimen/grid_item_height"
android:layout_marginBottom="@dimen/grid_item_margin"
android:layout_marginEnd="@dimen/grid_item_margin"
android:layout_marginLeft="@dimen/grid_item_margin"
android:layout_marginRight="@dimen/grid_item_margin"
android:layout_marginStart="@dimen/grid_item_margin"
android:layout_marginTop="@dimen/grid_item_margin"
android:background="@color/grid_items_background_color"
android:text="@string/thursday"
android:textAllCaps="false"
android:textColor="@android:color/black"
android:textSize="@dimen/grid_text_size"
android:gravity="center"
app:layout_column="0"
app:layout_columnWeight="1"
app:layout_gravity="fill"
app:layout_row="4"
app:layout_rowWeight="1" />
<TextView
android:id="@+id/item_14"
android:layout_width="wrap_content"
android:layout_height="@dimen/grid_item_height"
android:layout_marginBottom="@dimen/grid_item_margin"
android:layout_marginEnd="@dimen/grid_item_margin"
android:layout_marginLeft="@dimen/grid_item_margin"
android:layout_marginRight="@dimen/grid_item_margin"
android:layout_marginStart="@dimen/grid_item_margin"
android:layout_marginTop="@dimen/grid_item_margin"
android:background="@color/grid_items_background_color"
android:text=""
android:textAllCaps="false"
android:textColor="@android:color/black"
android:textSize="@dimen/grid_text_size"
android:gravity="center"
app:layout_column="1"
app:layout_columnWeight="1"
app:layout_gravity="fill"
app:layout_row="4"
app:layout_rowWeight="1" />
<TextView
android:id="@+id/item_15"
android:layout_width="wrap_content"
android:layout_height="@dimen/grid_item_height"
android:layout_marginBottom="@dimen/grid_item_margin"
android:layout_marginEnd="@dimen/grid_item_margin"
android:layout_marginLeft="@dimen/grid_item_margin"
android:layout_marginRight="@dimen/grid_item_margin"
android:layout_marginStart="@dimen/grid_item_margin"
android:layout_marginTop="@dimen/grid_item_margin"
android:background="@android:color/holo_red_light"
android:text=""
android:textAllCaps="false"
android:textColor="@android:color/black"
android:textSize="@dimen/grid_text_size"
android:gravity="center"
app:layout_column="2"
app:layout_columnWeight="1"
app:layout_gravity="fill"
app:layout_row="4"
app:layout_rowWeight="1" />
<TextView
android:id="@+id/item_16"
android:layout_width="wrap_content"
android:layout_height="@dimen/grid_item_height"
android:layout_marginBottom="@dimen/grid_item_margin"
android:layout_marginEnd="@dimen/grid_item_margin"
android:layout_marginLeft="@dimen/grid_item_margin"
android:layout_marginRight="@dimen/grid_item_margin"
android:layout_marginStart="@dimen/grid_item_margin"
android:layout_marginTop="@dimen/grid_item_margin"
android:background="@color/grid_items_background_color"
android:text="@string/friday"
android:textAllCaps="false"
android:textColor="@android:color/black"
android:textSize="@dimen/grid_text_size"
android:gravity="center"
app:layout_column="0"
app:layout_columnWeight="1"
app:layout_gravity="fill"
app:layout_row="5"
app:layout_rowWeight="1" />
<TextView
android:id="@+id/item_17"
android:layout_width="wrap_content"
android:layout_height="@dimen/grid_item_height"
android:layout_marginBottom="@dimen/grid_item_margin"
android:layout_marginEnd="@dimen/grid_item_margin"
android:layout_marginLeft="@dimen/grid_item_margin"
android:layout_marginRight="@dimen/grid_item_margin"
android:layout_marginStart="@dimen/grid_item_margin"
android:layout_marginTop="@dimen/grid_item_margin"
android:background="@android:color/holo_red_light"
android:text=""
android:textAllCaps="false"
android:textColor="@android:color/black"
android:textSize="@dimen/grid_text_size"
android:gravity="center"
app:layout_column="1"
app:layout_columnWeight="1"
app:layout_gravity="fill"
app:layout_row="5"
app:layout_rowWeight="1" />
<TextView
android:id="@+id/item_18"
android:layout_width="wrap_content"
android:layout_height="@dimen/grid_item_height"
android:layout_marginBottom="@dimen/grid_item_margin"
android:layout_marginEnd="@dimen/grid_item_margin"
android:layout_marginLeft="@dimen/grid_item_margin"
android:layout_marginRight="@dimen/grid_item_margin"
android:layout_marginStart="@dimen/grid_item_margin"
android:layout_marginTop="@dimen/grid_item_margin"
android:background="@color/grid_items_background_color"
android:text=""
android:textAllCaps="false"
android:textColor="@android:color/black"
android:textSize="@dimen/grid_text_size"
android:gravity="center"
app:layout_column="2"
app:layout_columnWeight="1"
app:layout_gravity="fill"
app:layout_row="5"
app:layout_rowWeight="1" />
</androidx.gridlayout.widget.GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
输出: