核心找到布局的根View 再找到子布局View 传递布局参数
lateinit var gameLayout:ConstraintLayout override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) arguments?.let { param1 = it.getInt("start") } } override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { val rootView=inflater.inflate(R.layout.fragment_game, container, false) gameLayout=rootView.findViewById(R.id.idGameClient) as ConstraintLayout return rootView }
fun GameFragment.Fg_id_sunning(){ //准备资源 gameLayout.removeAllViews() val btn= Button(activity) with(btn){ left=300 right=100 height=20 width=100 text="asddasdas" setBackgroundColor(Color.RED) } btn.layout(200,200,200,200) btn.setOnClickListener{ btn.text="fafasfdfa成功了" } gameLayout.addView(btn) //初始状态 //监控输入 //判断和保存状态 }