【问题标题】:ViewGroup.addView does not display anything on the screenViewGroup.addView 在屏幕上不显示任何内容
【发布时间】:2013-08-18 13:26:02
【问题描述】:

我正在为 Android 开发,但遇到了问题。当我使用ViewGroupaddView 方法添加ViewGroup 对象时,它不会在屏幕上显示任何内容。

谁能帮帮我?

RelativeLayout fullLayout=new RelativeLayout(this); 
RelativeLayout top=new RelativeLayout(this); 
TextView phoneNumber=new TextView(context); 
phoneNumber.setText("137928833"); 
RelativeLayout.LayoutParams phoneParams=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
phoneParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT); phoneParams.leftMargin=130; phoneParams.topMargin=120; 
top.addView(phoneNumber,phoneParams); 
fullLayout.addView(top,0); 

我使用 setContentView(fullLayouot),它不起作用。

【问题讨论】:

  • 请显示一些代码。你想做什么,你在哪里卡住了?
  • RelativeLayout fullLayout=new RelativeLayout(this);相对布局顶部=新的相对布局(这个); TextView phoneNumber=new TextView(context); phoneNumber.setText("137928833"); RelativeLayout.LayoutParams phoneParams=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); phoneParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT); phoneParams.leftMargin=130; phoneParams.topMargin=120; top.addView(phoneNumber,phoneParams); fullLayout.addView(top,0);我使用 setContentView(fullLayouot),它不起作用。
  • @xiaoxiaoniao:为什么不把上面的代码(注释)保留在你的问题本身,让大家看清楚你的问题

标签: android viewgroup


【解决方案1】:

它根本不起作用,因为您没有在顶级布局 (fullLayout) 上设置任何布局参数。没有宽度没有高度,什么都没有......

另一方面,您已经用通常应该通过异常的 ViewGroup 值实例化了相对布局参数。

【讨论】:

    【解决方案2】:

    我不太确定您要做什么;但不是以编程方式创建这些视图,您可以创建一个包含它们的 xml 并扩展该布局。以下是一些可能有用的其他问题:

    【讨论】:

    • 感谢您的回答,但我想动态创建这些视图而不是使用 xml。@Ryan Smith
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多