简体   繁体   English

以编程方式创建一个新的 TextView,然后将其显示在另一个 TextView 下方

[英]Create a new TextView programmatically then display it below another TextView

String[] textArray={"one","two","asdasasdf asdf dsdaa"};
int length=textArray.length;
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for(int i=0;i<length;i++){
    TextView tv=new TextView(getApplicationContext());
    tv.setText(textArray[i]);
    relativeParams.addRule(RelativeLayout.BELOW, tv.getId());
    layout.addView(tv, relativeParams);
}

I need to do something like that.. so it would display as我需要做这样的事情..所以它会显示为

one
two
asdfasdfsomething

on the screen..屏幕上..

If it's not important to use a RelativeLayout, you could use a LinearLayout, and do this:如果使用 RelativeLayout 并不重要,则可以使用 LinearLayout,然后执行以下操作:

LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);

Doing this allows you to avoid the addRule method you've tried.这样做可以避免您尝试过的 addRule 方法。 You can simply use addView() to add new TextViews.您可以简单地使用 addView() 添加新的 TextView。

Complete code:完整代码:

String[] textArray = {"One", "Two", "Three", "Four"};
LinearLayout linearLayout = new LinearLayout(this);
setContentView(linearLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL);        
for( int i = 0; i < textArray.length; i++ )
{
    TextView textView = new TextView(this);
    textView.setText(textArray[i]);
    linearLayout.addView(textView);
}

Try this code:试试这个代码:

final String[] str = {"one","two","three","asdfgf"};
final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
final TextView[] tv = new TextView[10];

for (int i=0; i<str.length; i++)
{
    tv[i] = new TextView(this); 
    RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams
         ((int)LayoutParams.WRAP_CONTENT,(int)LayoutParams.WRAP_CONTENT);
    params.leftMargin = 50;
    params.topMargin  = i*50;
    tv[i].setText(str[i]);
    tv[i].setTextSize((float) 20);
    tv[i].setPadding(20, 50, 20, 50);
    tv[i].setLayoutParams(params);
    rl.addView(tv[i]);  
}
public View recentView;

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Create a relative layout and add a button
        relativeLayout = new RelativeLayout(this);
        btn = new Button(this);
        btn.setId((int)System.currentTimeMillis());
        recentView = btn;
        btn.setText("Click me");
        relativeLayout.addView(btn);


        setContentView(relativeLayout);

        btn.setOnClickListener(new View.OnClickListener() {

            @Overr ide
            public void onClick(View view) {

                //Create a textView, set a random ID and position it below the most recently added view
                textView = new TextView(ActivityName.this);
                textView.setId((int)System.currentTimeMillis());
                layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
                layoutParams.addRule(RelativeLayout.BELOW, recentView.getId());
                textView.setText("Time: "+System.currentTimeMillis());
                relativeLayout.addView(textView, layoutParams);
                recentView = textView;
            }
        });
    }

This can be modified to display each element of a String array in different TextViews.这可以修改为在不同的 TextViews 中显示 String 数组的每个元素。

You're not assigning any id to the text view, but you're using tv.getId() to pass it to the addRule method as a parameter.您没有为文本视图分配任何 id,而是使用tv.getId()将其作为参数传递给addRule方法。 Try to set a unique id via tv.setId(int) .尝试通过tv.setId(int)设置一个唯一的 id。

You could also use the LinearLayout with vertical orientation, that might be easier actually.您还可以使用具有垂直方向的 LinearLayout,这实际上可能更容易。 I prefer LinearLayout over RelativeLayouts if not necessary otherwise.如果没有必要,我更喜欢 LinearLayout 而不是 RelativeLayouts。

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

相关问题 在相对布局中,如何在另一个textview下面显示运行时textview? - In a relative layout, how to display the run time textview below another textview? 使用现有textview设计以编程方式在另一个textview下面显示textview - Displaying textview below another textview programically using existing textview design Android-LayoutInflater-无法使textview停留在另一个textview之下 - Android - LayoutInflater - Cant make textview stay below another textview 如何创建一个新的 TextView 并显示它? (带编程) - How do I create a new TextView and display it? (with programming) TextView中的“ this”的角色textView = new TextView(this); - the role of “this” in TextView textView = new TextView(this); 当我按下按钮时,如何在另一个活动中创建一个新的 TextView? - How to create a new TextView in another activity when I press the button? 如何以编程方式将Android TextView对齐到另一个TextView的右侧 - How to programmatically align an Android TextView to the right of another TextView TextView 与另一个 textview 重叠 - TextView overlaps with another textview 在公共类中以编程方式创建一个新的 TextView - Creating a new TextView programmatically in a public class 以编程方式在textview的右侧创建并对齐edittext - Programmatically create and align an edittext to the right of a textview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM