【发布时间】:2020-06-14 16:00:08
【问题描述】:
我想创建一个 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 我了解到要整理行数并不容易。使用 API 16 使用 Grid 创建此类表的最简单方法是什么?
【问题讨论】:
-
ConstraintLayout 可以很好地解决这个问题。
-
嗨@Nicolas,你将如何使用它?
-
哦,您的意思是使用
ConstraintLayout而不是GridLayout?如果是这样,是不是可以用GridLayout来做,我更喜欢这样。