无法将视图添加到相对布局

时间:2016-11-10 10:44:00

标签: android android-layout android-studio

我正在创建一个从另一个活动获取int位置的活动,以获得具有公式,一些变量和标题的类公式,所以我想通过获取该公式并设置标题来创建活动,进行一些编辑对于变量的文本视图,以便用户为它们添加值,然后通过对话返回结果,问题是我不会显示我添加的视图,plz help

  public class DisplayFomula extends AppCompatActivity {

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_fomula);
    DB db = new DB(this);
    Bundle extra = getIntent().getExtras();
    int pos = extra.getInt("id");
    final Formula form = db.getFormula(pos);
    RelativeLayout layout=new RelativeLayout(this);
    TextView tw1 =(TextView) findViewById(R.id.textView11);
    tw1.setText(form.title);

    final String[] var = form.var;
    int i=0;
    final EditText[] ed = new EditText[50];
    for(;i<var.length;i++){
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
    ed[i]= new EditText(this);
    ed[i].setId(i);
        ed[i].setHint(var[i]);
        ed[i].setTextAlignment(View.TEXT_ALIGNMENT_CENTER);

    ed[i].setLayoutParams(params);
        if(i!=0) params.addRule(RelativeLayout.BELOW,ed[i-1].getId());
        else params.addRule(RelativeLayout.BELOW,R.id.textView11);
    layout.addView(ed[i],params);}
    Button btn = new Button(this);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    btn.setId(i);
    params.addRule(RelativeLayout.CENTER_IN_PARENT);
    btn.setLayoutParams(params);
    btn.setText("Go!");
    layout.addView(btn,params);
    params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT);
    addContentView(layout,params);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            double[] varin=new double[var.length];
            for(int i =0;i<var.length;i++){
                Double dbl = Double.parseDouble(ed[i].getText().toString());
                varin[i]=dbl;
            }
            String res = String.valueOf(form.result(varin));


        }
    });


}

}

1 个答案:

答案 0 :(得分:0)

您正在将所有视图添加到RelativeLayout布局。但我认为你没有为任何activity_display_fomula视图添加布局视图。 从activity_display_fomula获取一个viewgroup并将RelativeLayout布局添加到它..