Android TextView以编程方式跨越全宽

Posted

技术标签:

【中文标题】Android TextView以编程方式跨越全宽【英文标题】:Android TextView span full width programmatically 【发布时间】:2012-10-31 01:00:45 【问题描述】:

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

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

文本视图被缩小:

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


【问题讨论】:

请注意您的标签选择。我不知道你是怎么把“android”变成“adnrdoi”的…… 【参考方案1】:

对视图及其容器使用LayoutParams

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

【讨论】:

以上是关于Android TextView以编程方式跨越全宽的主要内容,如果未能解决你的问题,请参考以下文章

Android - 以编程方式设置 TextView TextStyle?

android:TextView 的 LayoutParams 以编程方式使视图消失

以编程方式 Textview 选取框

以编程方式将文本颜色设置为主要的 android textview

在 Android 的 Textview 中以编程方式居中文本

以编程方式在android中将下划线文本设置为TextView