从 Kotlin 填充 GridLayout
Fill a GridLayout from Kotlin
我是 android 工作室菜鸟。我正在尝试从 kotlin activity.
填充网格布局
我的 xml 代码是这样的:
<androidx.gridlayout.widget.GridLayout
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:rowCount="3"
app:columnCount="2">
</androidx.gridlayout.widget.GridLayout>
在 MainActivity 中有一个 for
可以像这样创建每个按钮:
val gridLayout: GridLayout = findViewById(R.id.grid)
for (i in 1..10){
val button = Button(this)
button.text = "Boton: " + i
// GridLayout.add(Buttom)
}
谁可以做?
谢谢
我应该把import android.widget.GridLayout
改成import androidx.gridlayout.widget.GridLayout;
并使用gridLayout.addView(button)
添加按钮
根据给定的代码修改您的函数。
for (i in 1..10){
val button = Button(this)
button.text = "Boton: " + i
val param = GridLayout.LayoutParams(
GridLayout.spec(
GridLayout.UNDEFINED, GridLayout.FILL, 1f
),
GridLayout.spec(GridLayout.UNDEFINED, GridLayout.FILL, 1f)
)
button.layoutParams = param
gridLayout.addView(button)
}
我是 android 工作室菜鸟。我正在尝试从 kotlin activity.
填充网格布局我的 xml 代码是这样的:
<androidx.gridlayout.widget.GridLayout
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:rowCount="3"
app:columnCount="2">
</androidx.gridlayout.widget.GridLayout>
在 MainActivity 中有一个 for
可以像这样创建每个按钮:
val gridLayout: GridLayout = findViewById(R.id.grid)
for (i in 1..10){
val button = Button(this)
button.text = "Boton: " + i
// GridLayout.add(Buttom)
}
谁可以做?
谢谢
我应该把import android.widget.GridLayout
改成import androidx.gridlayout.widget.GridLayout;
并使用gridLayout.addView(button)
添加按钮
根据给定的代码修改您的函数。
for (i in 1..10){
val button = Button(this)
button.text = "Boton: " + i
val param = GridLayout.LayoutParams(
GridLayout.spec(
GridLayout.UNDEFINED, GridLayout.FILL, 1f
),
GridLayout.spec(GridLayout.UNDEFINED, GridLayout.FILL, 1f)
)
button.layoutParams = param
gridLayout.addView(button)
}