添加的视图现在显示到继承的 LinearLayout

发布于 2024-11-08 07:31:26 字数 1518 浏览 2 评论 0原文

我有一个扩展 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沦落红尘 2024-11-15 07:31:26

我首先尝试使用层次结构查看器来查看视图树中的信息。您可能会在那里找到答案。

I would first try to use hierarchy viewer to see the info in your view tree. You will likely find your answer there.

仅一夜美梦 2024-11-15 07:31:26

默认生成的 onLayout 的重写没有调用它的 super。这解决了问题。

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
     super.onLayout(changed, l, t, r, b);       
}

the override for onLayout that was generated by default did not call it's super. this solved the problem.

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
     super.onLayout(changed, l, t, r, b);       
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文