0

我有以下代码,将 textCheckedView 添加到相对布局中:

public class MainActivity extends Activity {

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

        setContentView(R.layout.main_activity);


        final RelativeLayout rlayout = (RelativeLayout) findViewById(R.id.relativeLayout1);

        final RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        p.addRule(RelativeLayout.ALIGN_PARENT_TOP);

        final Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            int ids=0;
            public void onClick(View v) {


                String test ="test";
                ids++;

                final AlarmCheckedTextView checkedTV = createButton(test,ids);
                checkedTV.setVisibility(1);       
                p.addRule(RelativeLayout.BELOW,checkedTV.getId());

                rlayout.addView(checkedTV,p);  
            }
        });


    }




    private CustomCheckedTextView createButton(String text, int id)
    {
        final CustomCheckedTextView checkedTV = new CustomCheckedTextView(this,text);
        checkedTV.setId(id);
        return checkedTV;
    }

}

但是我CustomCheckedTextViewRelativeLayout点击Button. 我的意思是所有内容都已成功添加,但都在一个地方。如何以编程方式添加低于以前的元素?

4

1 回答 1

1

你可以这样理解:---

假设您要创建两个 TextView 并希望将一个 textview 下面放到另一个:-

    RelativeLayout relative = new RelativeLayout(this);
                TextView proposalA = new TextView(this);
                proposalA.setText("Proposal A:-");
                proposalA.setTextColor(Color.BLACK);
                proposalA.setId(R.id.propasal_a);//set id for this TextView you can put unique id for every content in your string folder otherwise you can set id like this:   tv1.setId((int)System.currentTimeMillis());
                proposalA.setTextSize(16);
                proposalA.setTypeface(Typeface.DEFAULT_BOLD);
                RelativeLayout.LayoutParams relative_params_a = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
                relative_params_a.setMargins(20, 6, 0, 0);

提案A.setLayoutParams(relative_params_a); 相对 .addView(proposalA); // 在您的主要相对布局中添加 textview 现在您想将 (textview)proposalB 下面放在 textview(ProposalA) 上,然后:- 对于第一个 TextView 下的第二个 TextView 使用addRule像这样:---

            TextView proposalB = new TextView(this);
            proposalB.setText("Proposal B:-");
            proposalB.setTextColor(Color.BLACK);
            proposalB.setId(R.id.propasal_b);   
            proposalB.setTextSize(16);
            proposalB.setTypeface(Typeface.DEFAULT_BOLD);
            RelativeLayout.LayoutParams relative_params_b = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
          relative_params_b.addRule(RelativeLayout.BELOW,proposalA.getId());

            relative_params.setMargins(20, 6, 0, 0);
           proposalB.setLayoutParams(relative_params_b);
        relative .addView(proposalB);
于 2013-06-24T11:21:03.797 回答