简体   繁体   中英

How can I set the position of a TextView using Java Code?

I'm still learning Android, so I know this question could sound silly. Here we go, I need to set the position of a large number of TextView's on my Activity. I receive the number using an variable that comes from an intent, but later when I need to create as many TextView's as the number is, they appear at the position (0,0) on my layout. I need to create a grill such as: __ __ __ __ But, all of them appear at the same place (0,0) and my application only shows: __

Here is the code I tried:

public class Play extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_play);


        Intent intent=getIntent();
        String n = intent.getStringExtra("NUM");
        int times = Integer.parseInt(n);

        TextView [] espaces = new TextView[times];

        for(int i = 0; i < espaces.length; i++){

            espaces[i] = new TextView(this);
            espaces[i].setText(" __ ");
            //Here I would like to set the position of my text view
            setContentView(espaces[i]);

        }

    }
}

You don't seem to understand what setContentView() does. It replaces the existing content view with the view passed in. So you aren't adding views, you'll always be showing the last one you set. If you want to show them all, add them to a LinearLayout and make that the view you setContentView() on.

If what i read is right i assume that you're using a relative layout you need to change to LinearLayout.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
</LinearLayout> 

Also you are adding view in the wrong way, you need a variable referencing the Linear Layout.

//you create a field outside the onCreate Method
LinearLayout layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_play);

        //then initialize the variable like this.
        layout = (LinearLayout) findViewById(R.id.linear_layout);

        Intent intent=getIntent();
        String n = intent.getStringExtra("NUM");
        int times = Integer.parseInt(n);

        TextView [] espaces = new TextView[times];

        for(int i = 0; i < espaces.length; i++){

            espaces[i] = new TextView(this);
            espaces[i].setText(" __ ");

            //you add like this
            layout.addView(espaces[i]);

        }

    }

I don't know content of layout xml act_play . But I hope it is LinearLayout with id container .

So change your code to next:

public class Play extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act_play);

    ViewGroup content = (ViewGroup)findViewById(R.id.container);  //<-this id from xml

    Intent intent=getIntent();
    String n = intent.getStringExtra("NUM");
    int times = Integer.parseInt(n);

    TextView [] espaces = new TextView[times];

    for(int i = 0; i < espaces.length; i++){

        espaces[i] = new TextView(this);
        espaces[i].setText(" __ ");
        content.addView(espaces[i]);//<-add to view group and not replace content view of activity

    }

}
}

BTW, you could put integer to intent, so no needs for conversion when you're getting it back.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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