gpt4 book ai didi

Android TextView以编程方式跨越全宽

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

我以编程方式在 LinearLayout 中创建了一个 TextView:

LinearLayout filmTitleContainer = new LinearLayout(ctx);
TextView filmName = new TextView(ctx);
filmName.setBackgroundColor(Color.rgb(Color.YELLOW));
filmName.setText(this.screeningTime+" "+this.name);

TextView 缩小了:

enter image description here

如何设置文本占满屏幕宽度?当我使用 XML 布局文件时,我会设置 android:layout_width="match_parent"。什么是等效的 Java 代码?


最佳答案

对 View 及其容器使用 LayoutParams:

LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f);
filmTitleContainer.setLayoutParams(params);
filmName.setLayoutParams(params);

enter image description here

关于Android TextView以编程方式跨越全宽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13329576/

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