У меня есть ConstraintLayout внутри RecyclerView, который живет во фрагменте. Я хочу изменить размеры ConstraintLayout (в зависимости от разрешения экрана), но не могу найти правильный способ получить ConstraintLayout в коде.
Я пробовал:
посмотреть !!. FindViewById (R.id.postConstraint)
активность !!. FindViewById (R.id.postConstraint)
Оба из которых возвращают ноль.
Мой фрагмент
package no.alacho.pgr202_positivity
import android.graphics.Color
import android.graphics.Point
import android.os.Bundle
import android.util.Log
import android.view.Display
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import kotlinx.android.synthetic.main.fragment_main.*
import kotlinx.android.synthetic.main.single_post_item.*
class Posts : Fragment() {
private lateinit var layoutManager: GridLayoutManager
private lateinit var adapter: PostAdapter
private lateinit var display: Display
private lateinit var size: Point
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
display = activity!!.windowManager.defaultDisplay
size = Point()
display.getSize(size)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_main, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
layoutManager = GridLayoutManager(activity, 2)
postRecycler.layoutManager = GridLayoutManager(activity, 2)
adapter = PostAdapter()
postRecycler.adapter = adapter
//view!!.findViewById<Button>(R.id.hurraButton).text = "Something"
view!!.findViewById<ConstraintLayout>(R.id.postConstraint).minWidth = 100
activity!!.findViewById<ConstraintLayout>(R.id.postConstraint).minWidth = 100
}
}
И мой XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
android:layout_height="wrap_content"
android:minHeight="150dp"
android:minWidth="150dp"
android:layout_width="wrap_content"
android:backgroundTint="#FF0000"
android:background="#FF0000"
android:id="@+id/postConstraint">
<Button android:layout_width="wrap_content"
android:id="@+id/hurraButton"
android:layout_height="wrap_content"
android:text="Hallo"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>