это код, который я использую для добавления вида в GridLayout
override fun setUPMatrix(payload: List<PayloadItem?>) {
var space = 10
var dataList: ArrayList<PayloadItem?> = payload as ArrayList<PayloadItem?>
var AnimatorSetList: ArrayList<AnimatorSet?> = ArrayList()
tvfreeCells.text = ""
titletext.text = ""
var Cellscounter: Int = 0
rvList.removeAllViews()
count++
var a = 0
var freeCells = ""
for (i in 0..5) {
a++
dataList.add((24 + i), PayloadItem("", "", 0, "", "", "", "", "", "", "", 0, "", ""))
}
val displayMetrics = DisplayMetrics()
activity?.getWindowManager()?.getDefaultDisplay()?.getMetrics(displayMetrics)
width = (rvList.width).toInt()
hight = (rvList.height).toInt()
var params: LinearLayout.LayoutParams? = null
for (i in 0..dataList.size - 1) {
var view: View
view = LayoutInflater.from(activity).inflate(R.layout.cells_layout, null)
val llmainCells = view.findViewById<LinearLayout>(R.id.llmainCells)
if (i >= 24 && i < 30) {
llmainCells.setBackgroundColor(Color.parseColor("#00000000"))
} else {
llmainCells.setBackground(resources.getDrawable(R.drawable.ic_rectangle_medium))
txtRow.setText(String.valueOf(dataList.get(i)?.id))
}
params =
if (dataList.get(i)?.cellSize?.equals(AppConstants.MEDIUM)!!) {
LinearLayout.LayoutParams(width / 6 - space, ((hight / 11)).toInt())
} else if (dataList.get(i)?.cellSize?.equals(AppConstants.LARGE)!!) {
LinearLayout.LayoutParams(width / 6 - space, ((hight / 11) * 1.30).toInt())
} else {
LinearLayout.LayoutParams(width / 6 - space, ((hight / 11) * 0.70).toInt())
}
params.setMargins(space)
llmainCells.setBackgroundColor(resources.getColor(R.color.liteGreen))
} else {
if (i >= 24 && i < 30) {
llmainCells.setBackgroundColor(Color.parseColor("#00000000"))
greenDot.visibility = View.INVISIBLE
} else {
llmainCells.setBackgroundColor(resources.getColor(R.color.dark_gray))
}
}
params.gravity = Gravity.CENTER
if (llmainCells.getParent() != null) {
(llmainCells.getParent() as ViewGroup).removeView(llmainCells)
}
llmainCells.visibility = View.VISIBLE
txtRow.visibility = View.VISIBLE
rvList.addView(llmainCells)
}
}
макет строки, который раздувает этот макет в gridlayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/_10sdp"
android:clickable="true"
android:gravity="center"
android:orientation="horizontal"
android:visibility="gone">
<ImageView
android:id="@+id/greenDot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/_5sdp"
android:src="@drawable/ic_green_dot"
android:visibility="invisible" />
<TextView
android:id="@+id/cellNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|center_horizontal"
android:layout_marginEnd="@dimen/_8sdp"
android:fontFamily="@font/ploni_medium_aaa"
android:text=""
android:textColor="@color/colorwhite"
android:textSize="@dimen/_8sdp"
android:visibility="invisible" />
</LinearLayout>
это мой основной макет
<FrameLayout android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ic_background"
tools:context=".ui.freeCells.FreeCellsFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<GridLayout
android:id="@+id/rvList"
android:background="@color/Black"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alignmentMode="alignBounds"
android:useDefaultMargins="true"
android:layout_below="@+id/tvfreeCells"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginStart="@dimen/_20sdp"
android:layout_marginTop="@dimen/_5sdp"
android:layout_marginEnd="@dimen/_20sdp"
android:layout_marginBottom="@dimen/_5sdp"
android:orientation="vertical"
android:rowCount="11" />
</RelativeLayout>
</FrameLayout>
и пространство, которое я установил в программе, работает в API уровня 21 или выше, но мне нужно работать в 4.4 в API уровня 19