用一行编程的方式在LinearLayout中定义TextView



我知道可以在多行中这样做:

    LinearLayout table=new LinearLayout(this);
    TextView titleText = new TextView(this);
    titleText.setText("Med Info");
    LinearLayout table=new LinearLayout(this);
    table.addView(titleText);

是否有办法在一行中做到这一点而不使用XML?我已经试过了:

LinearLayout table=new LinearLayout(this);
    table.addView(new TextView(this).setText("Med Info"));

不能工作(错误的返回值)。我检查了TextView构造函数,但似乎没有什么符合我的目标。我只是想缩短已经存在的代码,而不是一个严肃的项目。

不,如果不创建包装器函数,这是不可能的,因为.addView().setText()都返回void

无论如何你都不需要这样做。可读性远比压缩代码行重要。

最新更新