无法动态地将自定义布局附加到Activity

时间:2013-11-26 19:59:03

标签: android android-layout

当我在onCreate()中创建Activity并设置新的RelativeLayout时,一切正常。 但我认为创建一个扩展RelativeLayout并在那里进行所有设置的类MView更方便。如果我这样做,活动将保持空白。我应该在MView中更改什么(或者我需要在没有扩展名的情况下使用RelativeLayout)?

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

RelativeLayout mLayout = new RelativeLayout(this);//Change this to MView mLayout=new MView(this);
//I've been trying to move text from here...
TextView tv = new TextView(this);
tv.setText("TestText");
tv.setTextAppearance(this, android.R.attr.textAppearanceSmall);

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

tv.setLayoutParams(lp);

mLayout.addView(tv);
//... to here to MView class that extends RelativeLayout

RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.MATCH_PARENT,
        RelativeLayout.LayoutParams.MATCH_PARENT);

setContentView(mLayout, rlp);
}

因此,MView构造函数的代码是:

public MView(Context context) {
    super(context);
    TextView tv = new TextView(context);
    tv.setText("TestText");
    tv.setTextAppearance(context, android.R.attr.textAppearanceLarge);

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

    tv.setLayoutParams(lp);

    this.addView(tv);
}

onCreate成为了这个:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    MView mLayout = new MView(this);

    RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);

    setContentView(mLayout, rlp);
}

2 个答案:

答案 0 :(得分:0)

  1. 最好将布局参数设置为重定向视图,而不是在添加其他布局时。
  2. 在您的情况下,现在为文本设置错误的添加规则。
  3. 更正后的代码:

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            RelativeLayout mLayout = new RelativeLayout(this);
            RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.MATCH_PARENT);
    
            mLayout.setLayoutParams(rlp);
            TextView tv = new TextView(this);
            tv.setText("TestText");
            tv.setTextAppearance(this, android.R.attr.textAppearanceSmall);
            RelativeLayout.LayoutParams rlt = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            rlt.addRule(RelativeLayout.CENTER_IN_PARENT);
            tv.setLayoutParams(rlt);
    
            mLayout.addView(tv);
            setContentView(mLayout);
    }
    

答案 1 :(得分:0)

非常简单。当我创建MView时,我的onLayout实现是空的。显然,我不得不写

super.onLayout(arg0, arg1, arg2, arg3, arg4);

它工作正常。感谢您的帮助:)