gpt4 book ai didi

java - R.layout.activity_main 添加LinearLayout

转载 作者:太空宇宙 更新时间:2023-11-04 10:08:51 24 4
gpt4 key购买 nike

我有一个布局R.layout.activity_main,我必须向这个布局添加另一个布局,但使用java代码。

所以我实现了这样的东西:

LinearLayout layout = new LinearLayout(this);
LinearLayout.LayoutParams match_parent = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
layout.setBackgroundColor(Color.parseColor("#000000"));
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(match_parent);

所以对于R.layout.activity_main,我需要添加上面的代码。

我该怎么办?

最佳答案

在您的 Activity 中执行以下操作:

public void onCreate(Bundle savedInstanceState){
setContentView(R.layout.activity_main);
LinearLayout layout = new LinearLayout(this);
LinearLayout.LayoutParams match_parent = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
layout.setBackgroundColor(Color.parseColor("#000000"));
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(match_parent);
ViewGroup group = findViewById(R.id.id_where_you_wanna_add_your_layout);
group.addView(layout)
}

关于java - R.layout.activity_main 添加LinearLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52594298/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com