【问题标题】:Android: How can I make merging vertically?Android:如何进行垂直合并?
【发布时间】: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


【解决方案1】:

使用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>

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

  • android:columnCount
  • android:rowCount

你可以用这两个属性来定义一个View在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>

您还可以像这样以编程方式使用LayoutParams 填充GridLayout

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

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

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

【讨论】:

  • 有没有办法从源代码中动态添加wievs到GridLayout?像这样:假设变量 a 是 GridLayout a.addView(Button b);a.setColumn(4);a.setRow(2)。
  • 当然,您可以使用LayoutParams 定义我在上面谈到的所有xml 值,就像在RelativeLayout 中一样。如果你给我几分钟,我可以在我的答案中编辑一个示例。
  • 首先,感谢您的回答..但有一个问题。我认为这是我的错,但我找不到它。我无法在跨越的单元格中伸出按钮。我意思是按钮没有填满 3 个单元格
  • 位置是基于 0 的索引,但跨度不是,因此如果您有 8 列并且希望 View 跨越所有列,则您需要的 columnSpan 为 8,而不是 7。跨度>
  • 让我们假设有一台机器在星期一从 8 点到 11 点工作,所以我必须添加一个按钮,这应该填充 3 个单元格,我不想跨越所有这些单元格..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多