Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
895 views
in Technique[技术] by (71.8m points)

java - How to programmatically align an Android TextView to the right of another TextView

In the below code I have created two text views and added them programmatically to a relative layout. I want to align them side by side.

The code runs fine but is not placing the new TextView to the right of previous TextView instead the new TextView is positioned at margin (0,0,0,0) i.e. upper right corner of the screen:

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    relativeLayout= (RelativeLayout) findViewById(R.id.relative_Layout);

    textView[0] = new TextView(this);//creates first textview
    textView[0].setId(0);
    textView[0].setText("1");
    textView[0].setBackgroundResource(R.drawable.shape);//parses an image from shape.xml

    relativeLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
    textView[0].setLayoutParams(relativeLayoutParams);

    relativeLayout.addView(textView[0]);//creates another textview

    textView[1] = new TextView(this);

    textView[1].setBackgroundResource(R.drawable.shape);

    RelativeLayout.LayoutParams relativeLayoutParams=
            new RelativeLayout.LayoutParams((RelativeLayout.LayoutParams.WRAP_CONTENT),(RelativeLayout.LayoutParams.WRAP_CONTENT));//create params for new textview

    relativeLayoutParams.addRule(RelativeLayout.RIGHT_OF, textView[0].getId());//to align the textview side by side
    textView[1].setText("2");

    relativeLayout.addView(textView[1], relativeLayoutParams);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try the following:

  1. Set the id of textView[0] to 1 instead of 0 (id needs to be a positive integer)
  2. Add to the relativeLayoutParams of textView[1] a rule for RelativeLayout.ALIGN_TOP

The following worked for me:

RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.test);
RelativeLayout.LayoutParams relativeLayoutParams;       
TextView[] textView = new TextView[2];

// 1st TextView
textView[0] = new TextView(this);

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

textView[0].setId(1); // changed id from 0 to 1
textView[0].setText("1");   

relativeLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);

relativeLayout.addView(textView[0], relativeLayoutParams);

// 2nd TextView
textView[1] = new TextView(this);

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

textView[1].setText("2");

relativeLayoutParams.addRule(RelativeLayout.RIGHT_OF,
        textView[0].getId());
relativeLayoutParams.addRule(RelativeLayout.ALIGN_TOP,
        textView[0].getId()); // added top alignment rule

relativeLayout.addView(textView[1], relativeLayoutParams);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...