gpt4 book ai didi

android - 如何设置android :layout_width ="match_parent" from code?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:21:39 27 4
gpt4 key购买 nike

我有不同的布局。一些由 xml 创建。其他一些动态地通过代码。当我在 xml 上时,我可以使用“wrap_content”值设置宽度或高度。如何动态获得相同的结果?这是我的动态 TextView 的 fragment 。我需要删除“final int width = 440;”并获得相同的“wrap_content”值。怎么办?

final int width = 440;
final int height = textViewHeight;
final int top = getNewTop(height);

FrameLayout.LayoutParams layoutParams;
layoutParams = getLayoutParams(width, height, top);

TextView textView;
textView = new TextView(_registerNewMealActivity);
textView.setText(text);
textView.setLayoutParams(layoutParams);

_frameLayout.addView(textView);

最佳答案

你可以使用:

textView.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, height));

关于android - 如何设置android :layout_width ="match_parent" from code?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20512854/

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