Android:如何进行垂直合并?

sof*_*eer 2 android android-layout android-linearlayout android-fragments android-activity

我想做一个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" 
    />
Run Code Online (Sandbox Code Playgroud)

这是主要活动

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);

}
Run Code Online (Sandbox Code Playgroud)

我正在为机器做一个程序,列代表那台机器的工作天数,行代表那台机器的工作小时数。因此,我必须将一个按钮从开始时间延长到结束时间,但我无法做到这一点。

Xav*_*ler 5

使用一个GridLayout!它是这样工作的:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="4"
    android:rowCount="4"
    android:orientation="horizontal">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:layout_row="1"
        android:layout_columnSpan="2"
        android:layout_rowSpan="2" />

    ...

</GridLayout>
Run Code Online (Sandbox Code Playgroud)

要定义有多少行和列,请使用 GridLayout 上的这两个属性:

  • android:columnCount
  • android:rowCount

您可以使用以下两个属性在 GridLayout 中定义视图的位置:

  • android:layout_column
  • android:layout_row

您可以使用以下两个属性定义视图跨越的行数或列数:

  • android:layout_rowSpan
  • android:layout_columnSpan

请注意,GridLayout 仅在 API 级别 14 中添加!如果要在早期版本中使用 GridLayout,则需要使用支持库中的 GridLayout:

<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    ...    

</android.support.v7.widget.GridLayout>
Run Code Online (Sandbox Code Playgroud)

您还可以像这样以GridLayout编程方式填充一个LayoutParams

final int row = 1;  // View starts in second row
final int rowSpan = 2; // View spans two rows

final int column = 1; // View starts in second column
final int columnSpan = 2; // View spans two columns

// Creating the LayoutParams from the values above
GridLayout.LayoutParams params = new GridLayout.LayoutParams();
params.width = GridLayout.LayoutParams.WRAP_CONTENT;
params.height = GridLayout.LayoutParams.WRAP_CONTENT;
params.columnSpec = GridLayout.spec(column, columnSpan);
params.rowSpec = GridLayout.spec(row, rowSpan);

// Adding the new View with the LayoutParams
gridLayout.addView(view, params);
Run Code Online (Sandbox Code Playgroud)

我建议你添加一个辅助方法来简化这个:

private void addViewToGridLayout(View view, int row, int column, int rowSpan, int columnSpan) {
    GridLayout.LayoutParams params = new GridLayout.LayoutParams();
    params.width = GridLayout.LayoutParams.WRAP_CONTENT;
    params.height = GridLayout.LayoutParams.WRAP_CONTENT;
    params.columnSpec = GridLayout.spec(column, columnSpan);
    params.rowSpec = GridLayout.spec(row, rowSpan);

    gridLayout.addView(view, params);
}
Run Code Online (Sandbox Code Playgroud)