gpt4 book ai didi

java - 如何在java代码中使用wrap_cotent创建LinearLayout?

转载 作者:行者123 更新时间:2023-12-01 15:26:15 25 4
gpt4 key购买 nike

什么是 Java 编程等效于创建以下 XML 文件:

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout />
</LinearLayout>

我尝试这样做,但LinearLayout如何抓取MATCH_PARENT

// child is the RelativeLayout in the above example:
LinearLayout parent = new LinearLayout(getContext());
LinearLayout.LayoutParams childLayoutParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
parent.addView(child, -1, childLayoutParams);

最佳答案

试试这个

LinearLayout parent = new LinearLayout(getContext());
LayoutParams parentLayoutParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
parent.setLayoutParams(parentLayoutParams);

然后将其添加到屏幕

outerlayout.addView(parent);

关于java - 如何在java代码中使用wrap_cotent创建LinearLayout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10098165/

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