简体   繁体   English

如何使用Java代码设置TextView的位置?

[英]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. 我仍在学习Android,所以我知道这个问题听起来很愚蠢。 Here we go, I need to set the position of a large number of TextView's on my Activity. 到这里,我需要在Activity上设置大量TextView的位置。 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. 我使用来自意图的变量来接收数字,但是稍后当我需要创建与数字一样多的TextView时,它们将出现在布局中的位置(0,0)。 I need to create a grill such as: __ __ __ __ But, all of them appear at the same place (0,0) and my application only shows: __ 我需要创建一个格栅,例如:__ __ __ __但是,它们全部都出现在同一位置(0,0),而我的应用程序仅显示:__

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. 您似乎不了解setContentView()作用。 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. 如果要全部显示它们,请将它们添加到LinearLayout并在其上设置setContentView()的视图。

If what i read is right i assume that you're using a relative layout you need to change to LinearLayout. 如果我没看错,我假设您使用的是相对布局,则需要更改为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 . 我不知道布局xml act_play内容。 But I hope it is LinearLayout with id container . 但我希望它是带有id container LinearLayout

So change your code to next: 因此,将代码更改为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. 顺便说一句,您可以将整数放在意向中,因此找回它时无需进行转换。

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

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