CustomView扩展RelativeLayout

时间:2013-10-31 19:46:20

标签: android user-interface android-custom-view

我正在尝试创建一个自定义视图,允许我添加图像和文本。我这样做的原因是因为在我创建的应用程序中我反复重复相同的代码以实现这一点,我想尝试将其包装到自己的自定义视图中,这样我就可以使用setter和position相对于彼此的视图,将所有这些代码放在一个简单易用的类中。

我可以在视图中添加文本视图和图像视图,但是当我尝试将它们相互关联时,我总是会遇到NPE崩溃。

这是我到目前为止的代码,它只是添加一个TextView,然后试图将TextView置于我的自定义视图中。

public class MultiView extends RelativeLayout {
    Context cx;
    int images = 0;

    public MultiView(Context context) {

        super(context);
        cx = context;
    }

    public void addText(String textParam) {

        TextView tv = new TextView(cx);

        tv.setText(textParam);

        tv.setTextColor(Color.WHITE);

        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) tv
                .getLayoutParams();

        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);

//      tv.setLayoutParams(layoutParams); // the app crashes when I add this line

        this.addView(tv);
    }

}

3 个答案:

答案 0 :(得分:2)

全班:

public class MultiView extends RelativeLayout {
    Context cx;
    int images = 0;

    public MultiView(Context context) {

        super(context);
        cx = context;
    }

    public void addText(String textParam) {

        RelativeLayout.LayoutParams layoutParams = 
        new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,      
            RelativeLayout.LayoutParams.WRAP_CONTENT);


        TextView tv = new TextView(cx);
        tv.setText(textParam);
        tv.setTextColor(Color.WHITE);
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        tv.setLayoutParams(layoutParams);
        this.addView(tv);
    }

}

答案 1 :(得分:1)

你在那条线上遇到崩溃是非常奇怪的:

tv.setLayoutParams(layoutParams);

我认为你在那条线上得到它,因为创建的View没有任何LayoutParams(此外,如果它是RelativeLayout.LayoutParams):

layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);

我认为您可以通过DevBytes的视频改进addText()方法: http://www.youtube.com/watch?v=55wLsaWpQ4g

但是如果你想让你的代码工作,你需要替换这样的步骤:

TextView tv = new TextView(cx);
tv.setText(textParam);
tv.setTextColor(Color.WHITE);
this.addView(tv); // Programatically created View get LayoutParams when we add it to a ViewGroup

RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) tv.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
tv.setLayoutParams(layoutParams);

或者自己创建你的布局参数:

TextView tv = new TextView(cx);
tv.setText(textParam);
tv.setTextColor(Color.WHITE);

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
tv.setLayoutParams(layoutParams);

this.addView(tv);

答案 2 :(得分:1)

我认为您获得NPE,因为tv.getLayoutParams();会返回null

尝试以下方法:

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);

tv.setLayoutParams(layoutParams); // the app crashes when I add this line

this.addView(tv);