简体   繁体   English

android以编程方式创建RelativeLayout

[英]android creating a RelativeLayout programmatically

I'm having some troubles creating a relativeLayout programmatically.For better understanding I attached a picture below. 我有一些麻烦以编程方式创建relativeLayout。为了更好地理解,我附上了一张图片。 The things with plus and minus are buttons, which should also be dynamically created and added to the layout. 带加号和减号的东西是按钮,它们也应该动态创建并添加到布局中。 The values of tv4 and tv5 should increase/decrease accordingly to button presses. tv4和tv5的值应相应按钮按下增加/减少。

What I have done so far: 到目前为止我做了什么:

1) creating the layout: 1)创建布局:

RelativeLayout rl = new RelativeLayout(this);
    rl.setId(i);
    rl.setBackgroundResource(R.drawable.bg);
 RelativeLayout.LayoutParams Lparams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        Lparams.addRule(RelativeLayout.BELOW, R.id.RL_default);
        Lparams.setMargins(3, 5, 3, 0);
        rl.setLayoutParams(Lparams);

2) adding the tv1: 2)添加tv1:

  Lparams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    Lparams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
    Lparams.setMargins(10, 0, 0, 0);

    TextView txt = new TextView(this);
    txt.setTextColor(Color.parseColor("#FFFFFF"));
    txt.setId(x);
    txt.setTextSize(25);
    txt.setLayoutParams(Lparams);
    txt.setText(name);
            rl.addView(txt);

3) adding the tv2: 3)添加tv2:

           Lparams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    Lparams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
    Lparams.addRule(RelativeLayout.BELOW, txt.getId());
    Lparams.setMargins(10, 0, 0, 0);

    TextView txtS = new TextView(this);
    txtS.setId(y);
    txtS.setText("Test: ");
    txtS.setTextSize(22);

    txtS.setLayoutParams(Lparams);
    txtS.setGravity(Gravity.BOTTOM);
    txtS.setPadding(0, 0, 0, 20);
            rl.addView(txtS);

4) now I want to create the first button: 4)现在我要创建第一个按钮:

           Button btnSminus = new Button(this);
    btnSminus.setId(btn1);
    btnSminus.setText("<");
    btnSminus.setTextSize(20);

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

    Lparams.addRule(RelativeLayout.RIGHT_OF, txtS.getId());


    btnSminus.setLayoutParams(Lparams);
            rl.addView(btnSminus);

The problem is, the the button View seems to just disappear from the screen when the line Lparams.addRule(RelativeLayout.RIGHT_OF, txtS.getId()); 问题是,当Lparams.addRule(RelativeLayout.RIGHT_OF,txtS.getId())行时,按钮View似乎只是从屏幕上消失了; is executed. 被执行。 What can be the reason? 可能是什么原因?

在此输入图像描述

My guess would be that it's because you set the width of txtS to match_parent , so the button gets pushed off the screen. 我的猜测是因为你将txtS的宽度设置为match_parent ,所以按钮被推离屏幕。 You can fix that by changing that to wrap_content . 您可以通过将其更改为wrap_content来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM