android-layout - match_parent 不填充父级!

标签 android-layout

我在 TableRow 中有 LinearLayout。
LinearLayout 在代码中启动,这样:

LinearLayout  mainRowLayout = new LinearLayout(this);
mainRowLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
TableRow tr = new TableRow(this);
tr.addView(mainRowLayout);

问题是 LinearLayout 没有填充父级(即 TableRow)。
所附图片说明了这个问题,如 Android 的 hirarchyViewer 所示(绿色矩形是我的标记)。

"LinearLayout image"

谢谢。

最佳答案

以编程方式创建布局时,您需要为父容器提供子容器的布局说明。

// child element
LinearLayout mainRowLayout = new LinearLayout(this);

// parent element
TableRow tr = new TableRow(this);

// parent element's instructions (layout params for main row)
TableRow.LayoutParams lopForMainRow = new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

tr.addView(mainRowLayout, lopForMainRow);

搏一搏 :]

关于android-layout - match_parent 不填充父级!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4602882/

相关文章:

Android 选项卡 - 禁用点击时在选项卡之间切换

Android:以编程方式使用 fragment

java - 使用我的自定义类对象填充多列 ListView - Android

android - 所有屏幕尺寸的资源文件夹名称

java - 图形布局的 1/4 显示模拟时的情况

android - recyclerview 中的最后一项被切断

android - 如何使用 layoutinflator 在运行时添加 View ?

android - 无法将 ViewPager 放入 ScrollVIew/NestedScrollView

android - View 未在布局中心对齐

java - TextView setText View 中的空指针异常被夸大