【问题标题】:Create a new TextView programmatically then display it below another TextView以编程方式创建一个新的 TextView,然后将其显示在另一个 TextView 下方
【发布时间】:2011-05-22 14:41:42
【问题描述】:
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);
}

我需要做类似的事情......所以它会显示为

one
two
asdfasdfsomething

在屏幕上..

【问题讨论】:

  • 简单来说,你的代码有什么问题?什么不起作用?顺便说一句,如果这是一个 Activity,只需使用 this 而不是 getApplicationContext()
  • 您的问题标题显示“在下面另一个文本视图中显示”,但您的问题表述方式不同,所有文本都在一行中 - 请相应地澄清或格式化问题文本。跨度>
  • 问题的格式不正确 - 我已解决。 (换行符被吞掉可能并不直观)
  • 使用线性布局而不是相对布局

标签: java android textview android-relativelayout


【解决方案1】:

试试这个代码:

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]);  
}

【讨论】:

  • 如何在activity_main.xml文件中定义rl?
【解决方案2】:

如果使用 RelativeLayout 并不重要,您可以使用 LinearLayout,然后这样做:

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

这样做可以避免您尝试过的 addRule 方法。您可以简单地使用 addView() 添加新的 TextView。

完整代码:

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);
}

【讨论】:

  • 不要使用应用程序上下文来创建视图。使用活动上下文。
【解决方案3】:
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;
            }
        });
    }

这可以修改为在不同的 TextView 中显示 String 数组的每个元素。

【讨论】:

  • 我在将这个逻辑用于多个视图时遇到了问题,因为多个项目被分配了相同的 id,我用计数器更改了当前时间来解决问题
【解决方案4】:

您没有为文本视图分配任何 id,但您使用 tv.getId() 将其作为参数传递给 addRule 方法。尝试通过tv.setId(int)设置一个唯一的id。

您也可以使用垂直方向的 LinearLayout,这实际上可能更容易。如果没有必要,我更喜欢 LinearLayout 而不是 RelativeLayouts。

【讨论】:

  • 如何以编程方式分配唯一 ID?我的意思是如何确保它是唯一的,当你永远不会知道 xml 中 id 的值以及即将到来的 id 时
  • 你会知道xml中id的值,为什么不呢?制作像thisKindofElem001thisKindofElem002 等ID。
猜你喜欢
  • 2019-04-11
  • 2015-12-06
  • 1970-01-01
  • 2017-12-02
  • 1970-01-01
  • 1970-01-01
  • 2013-11-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多