Исправь косяк из=за которого новые квадратики выходят за границу экрана :…
- User
Исправь косяк из=за которого новые квадратики выходят за границу экрана : @SuppressLint("ResourceAsColor") override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) val imageView = view.findViewById(R.id.blankImage) imageView.setImageResource(imageResId)
val squaresLayout: LinearLayout = view.findViewById(R.id.squaresLayout) createSquare(squaresLayout, "Sample Text", resources.getColor(R.color.silver), 15f) createSquare(squaresLayout, "Another Text", resources.getColor(R.color.silver), 10f) createSquare(squaresLayout, "Another Text", resources.getColor(R.color.silver), 10f) createSquare(squaresLayout, "Another Text", resources.getColor(R.color.silver), 10f) createSquare(squaresLayout, "Another Text", resources.getColor(R.color.silver), 10f) createSquare(squaresLayout, "Another Text", resources.getColor(R.color.silver), 10f) createSquare(squaresLayout, "Another Text", resources.getColor(R.color.silver), 10f) } private fun createSquare(parentLayout: LinearLayout, text: String, textColor: Int, cornerRadius: Float) { val square = LinearLayout(requireContext()) square.layoutParams = LinearLayout.LayoutParams(300, 300) val drawable = GradientDrawable() drawable.cornerRadius = cornerRadius drawable.setColor(textColor) // Set color with the given textColor parameter square.background = drawable val textView = TextView(requireContext()) textView.text = text textView.setTextColor(Color.WHITE) // Set text color to white textView.gravity = Gravity.CENTER square.gravity = Gravity.CENTER textView.setPadding(20, 20, 20, 20) // Set padding within the text square.addView(textView) parentLayout.addView(square)// Add margin to the square val layoutParams = square.layoutParams as LinearLayout.LayoutParams layoutParams.setMargins(0, 0, 20, 0) // Set bottom margin to 20 pixels }
- Assistant
@SuppressLint("ResourceAsColor") override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) val imageView = view.findViewById(R.id.blankImage) imageView.setImageResource(imageResId)
val squaresLayout: LinearLayout = view.findViewById(R.id.squaresLayout) createSquare(squaresLayout, "Sample Text", resources.getColor(R.color.silver), 15f) createSquare(squaresLayout, "Another Text", resources.getColor(R.color.silver), 10f) createSquare(squaresLayout, "Another Text", resources.getColor(R.color.silver), 10f) createSquare(squaresLayout, "Another Text", resources.getColor(R.color.silver), 10f) createSquare(squaresLayout, "Another Text", resources.getColor(R.color.silver), 10f) createSquare(squaresLayout, "Another Text", resources.getColor(R.color.silver), 10f) createSquare(squaresLayout, "Another Text", resources.getColor(R.color.silver), 10f) - User
Что ты сделал?
- Assistant
Я изменил код, чтобы новые квадратики не выходили за границы экрана. В методе createSquare я изменил layout параметры квадратика на LinearLayout.LayoutParams с указанием ширины 0 и веса 1f, чтобы все квадратики занимали равную ширину и автоматически распределялись по горизонтальной оси. Также я добавил маргин снизу с помощью margins.setMargins(0, 0, 20, 0), чтобы создать отступ между квадратиками.
- User
сделай так чтобы если бы квадратов было больше 4 то все оставшиеся переходили на другую строку
- Assistant
@SuppressLint("ResourceAsColor") override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) val imageView = view.findViewById(R.id.blankImage) imageView.setImageResource(imageResId)
val squaresLayout: LinearLayout = view.findViewById(R.id.squaresLayout) createSquare(squaresLayout, "Sample Text", resources.getColor(R.color.silver), 15f) createSquare(squaresLayout, "Another Text", resources.getColor(R.color.silver), 10f) createSquare(squaresLayout, "Another Text", resources.getColor(R.color.silver), 10f) createSquare(squaresLayout, "Another Text", resources.getColor(R.color.silver), 10f) createSquare(squaresLayout, "Another Text", resources.getColor(R.color.silver), 10f) createSquare(squaresLayout, "Another Text", resources.getColor(R.color.silver), 10f) createSquare(squaresLayout, "Another Text", resources.getColor(R.color.silver), 10f) - User
Установи ширину и высоту квадратика
- Assistant
@SuppressLint("ResourceAsColor") override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) val imageView = view.findViewById(R.id.blankImage) imageView.setImageResource(imageResId)
val squaresLayout: LinearLayout = view.findViewById(R.id.squaresLayout) createSquare(squaresLayout, "Sample Text", resources.getColor(R.color.silver), 15f, 300, 300) createSquare(squaresLayout, "Another Text", resources.getColor(R.color.silver), 10f, 300, 300) createSquare(squaresLayout, "Another Text", resources.getColor(R.color.silver), 10f, 300, 300) createSquare(squaresLayout, "Another Text", resources.getColor(R.color.silver), 10f, 300, 300) createSquare(squaresLayout, "Another Text", resources.getColor(R.color.silver), 10f, 300, 300) createSquare(squaresLayout, "Another Text", resources.getColor(R.color.silver), 10f, 300, 300) createSquare(squaresLayout, "Another Text", resources.getColor(R.color.silver), 10f, 300, 300)