【发布时间】:2011-12-12 15:05:34
【问题描述】:
我有一个自定义组件,我知道宽度和高度始终如下:
android:layout_width="fill_parent"
android:layout_height="wrap_content"
我想避免在 XML 中指定宽度和高度,因为任何其他设置都是错误的。
我尝试了很多东西,但没有任何效果。例如,这段代码不起作用:
public class MyLayout extends ViewGroup {...
LayoutParams layoutParams =
new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
setLayoutParams(layoutParams);
但无论我做什么,我都会收到异常消息“您必须提供 layout_width 属性”。如果我放回 XML layout_width、layout_height,它就会消失。
我也尝试过覆盖onFinishInflate(),但从未被调用。我尝试从View、ViewGroup 和TableLayout 继承。我试过调用getLayoutParams(),但它返回null。
如何让我的自定义组件不需要在 XML 中指定这些?
请不要恶作剧。
谢谢。
【问题讨论】:
-
试试这个androidactivity.wordpress.com/2011/10/04/… 而不是 setWidth 试试 setMinWidth 希望它有帮助
标签: android view custom-controls android-layout tablelayout