Android TextView以编程方式跨越全宽

标签 android textview width

我以编程方式在 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/

相关文章:

android tabs - 开始一个新的 Activity

用于 GPS 服务的 Android 唤醒锁

jquery - FlexSlider 2 - 无法制作全宽、左右空白

CSS - 水平列表的动态宽度(在列表的列表列表中)

css - Foundation 5 全 Angular 标题图片

java - Android studio 处理多个用户。如何修改布局的可点击参数?

java - 自定义微调器适配器中未显示图像显示

android - utf-8 到字符串获取额外添加的字符

ios - 当在最后一行之前+该行最后一个字符之后开始换行时,光标总是跳到 UIViewRepresentable TextView 的末尾

java - 如何更改句子中两个特定单词之间的单词颜色