【发布时间】:2014-07-16 09:38:45
【问题描述】:
我想做一个 Android 应用程序。这个应用程序需要 7 行 7 列,如 7*7 矩阵。以下是问题:
1) 如何将单元格 1*1 与 2*1 或 1*1,2*1 和 3*1 垂直合并? 2)我也可以动态制作吗? 3)我应该使用哪种布局?
提前谢谢你。
这里是screen.xml
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="8"
android:orientation="horizontal"
android:rowCount="9" >
<Space
android:layout_column="0"
android:layout_columnSpan="1"
android:layout_gravity="fill"
android:layout_row="0"
android:tag=""/>
<TextView
android:id="@+id/Monday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_gravity="center"
android:layout_row="0"
android:text="Monday"
android:textSize="10dp"
android:tag="Monday"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_gravity="center"
android:layout_row="0"
android:text="Tuesday"
android:textSize="10dp"
android:tag="Tuesday"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_gravity="center"
android:layout_row="0"
android:text="Wednesd."
android:textSize="10dp"
android:tag="Wednesday"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="4"
android:layout_gravity="center"
android:layout_row="0"
android:text="Thursday"
android:textSize="10dp"
android:tag="Thursday"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="5"
android:layout_gravity="center"
android:layout_row="0"
android:text="Friday"
android:textSize="10dp"
android:tag="Friday"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="6"
android:layout_gravity="center"
android:layout_row="0"
android:text="Saturday"
android:textSize="10dp"
android:tag="Saturday"/>
<Space
android:layout_column="0"
android:layout_columnSpan="1"
android:layout_gravity="fill"
android:layout_row="0"
android:tag=""/>
<TextView
android:id="@+id/eight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="center"
android:layout_row="1"
android:text="8:00"
android:textSize="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="center"
android:layout_margin="5dp"
android:layout_row="2"
android:text="8:30"
android:textSize="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="center"
android:layout_margin="5dp"
android:layout_row="3"
android:text="9:00"
android:textSize="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="center"
android:layout_margin="5dp"
android:layout_row="4"
android:text="9:30"
android:textSize="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="center"
android:layout_margin="5dp"
android:layout_row="5"
android:text="10:00"
android:textSize="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="center"
android:layout_margin="5dp"
android:layout_row="6"
android:text="10:30"
android:textSize="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="center"
android:layout_margin="5dp"
android:layout_row="7"
android:text="11:00"
android:textSize="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="center"
android:layout_margin="5dp"
android:layout_row="8"
android:text="11:30"
android:textSize="10dp"
/>
这里是mainActivity
public class Example extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen);
Button lec = new Button(this);
GridLayout gridLayout = (GridLayout)findViewById(R.id.grid);
GridLayout.LayoutParams params = new GridLayout.LayoutParams();
params.width = GridLayout.LayoutParams.MATCH_PARENT;
params.height = GridLayout.LayoutParams.WRAP_CONTENT;
params.columnSpec = GridLayout.spec(1, 3);
params.rowSpec = GridLayout.spec(1, 7);
gridLayout.addView(lec, params);
}
我正在为机器做一个程序,列代表该机器的工作天数,行代表该机器的工作小时数。因此,我必须将一个按钮从开始时间延长到结束时间,但我无法做到这一点。
【问题讨论】:
-
你已经尝试过了吗?发布您的代码:)
-
您应该使用表格布局并根据需要合并它们的单元格
-
@Prag 的
TableLayout在这里不起作用。TableLayout仅支持列跨度,不支持行跨度,因此您无法按照 OP 的要求垂直合并单元格。但是GridLayout支持行跨度和列跨度,有关更多信息,请参阅我的答案。 -
好的..谢谢你的信息:)
-
您的问题解决了吗,还是需要进一步的帮助?请不要忘记接受对您最有帮助的答案!
标签: android android-layout android-activity android-fragments android-linearlayout