添加的视图现在显示到继承的 LinearLayout
我有一个扩展 LinearLayout 的类,我在构造函数中添加了一些视图。但是我添加的视图没有显示。我做错了什么?
我将背景资源设置为绿色。我确实看到了一个大的绿色方块。所以布局本身就显现出来了。
谢谢!
import android.content.Context;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class SuperButton extends LinearLayout
{
private ImageView buttonImage;
private TextView label;
private Context context;
public SuperButton(Context context,
String text,
int imageResource,
int backgroundResource)
{
super(context);
this.context = context;
this.setBackgroundResource(backgroundResource);
this.label = new TextView(context);
this.label.setText("test test test ");
this.label.setBackgroundColor(0xff0f0f0f);
this.buttonImage = new ImageView(context);
this.buttonImage.setImageResource(imageResource);
this.buttonImage.setLayoutParams(new
LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
this.label.setLayoutParams(new LayoutParams(100,100));
this.addView(label);
this.addView(buttonImage);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
}
public void setButtonImage(ImageView buttonImage)
{
this.buttonImage = buttonImage;
}
public ImageView getButtonImage()
{
return buttonImage;
}
public void setLabel(TextView label)
{
this.label = label;
}
public TextView getLabel()
{
return label;
}
}
I have a class that extends LinearLayout that I add some views to in the constructor. However the views I add don't show up. What am I doing wrong?
I set the background resource to the color green. And I do see a large green square. So the layout is showing up itself.
thanks!
import android.content.Context;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class SuperButton extends LinearLayout
{
private ImageView buttonImage;
private TextView label;
private Context context;
public SuperButton(Context context,
String text,
int imageResource,
int backgroundResource)
{
super(context);
this.context = context;
this.setBackgroundResource(backgroundResource);
this.label = new TextView(context);
this.label.setText("test test test ");
this.label.setBackgroundColor(0xff0f0f0f);
this.buttonImage = new ImageView(context);
this.buttonImage.setImageResource(imageResource);
this.buttonImage.setLayoutParams(new
LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
this.label.setLayoutParams(new LayoutParams(100,100));
this.addView(label);
this.addView(buttonImage);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
}
public void setButtonImage(ImageView buttonImage)
{
this.buttonImage = buttonImage;
}
public ImageView getButtonImage()
{
return buttonImage;
}
public void setLabel(TextView label)
{
this.label = label;
}
public TextView getLabel()
{
return label;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我首先尝试使用层次结构查看器来查看视图树中的信息。您可能会在那里找到答案。
I would first try to use hierarchy viewer to see the info in your view tree. You will likely find your answer there.
默认生成的 onLayout 的重写没有调用它的 super。这解决了问题。
the override for onLayout that was generated by default did not call it's super. this solved the problem.