创建的LinearLayout在Java中 - 元素不显示素不、LinearLayout、Java

由网友(烟花再美只是瞬间)分享简介:我想创建在Java TextViews一个LinearLayout中,因为元素的数量是动态指定所以使用XML将无法工作对我来说。这里是我的code一点点样本:I am trying to create a LinearLayout with TextViews in Java, because the number...

我想创建在Java TextViews一个LinearLayout中,因为元素的数量是动态指定所以使用XML将无法工作对我来说。 这里是我的code一点点样本:

I am trying to create a LinearLayout with TextViews in Java, because the number of elements is dynamically specified so using XML won't work out for me. Here is a little sample of my Code:

public class MyActivity extends Activity {

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

    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);
    layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));


    TextView titleView = new TextView(this);
    titleView.setWidth(LayoutParams.WRAP_CONTENT);
    titleView.setHeight(LayoutParams.WRAP_CONTENT);
    titleView.setTextAppearance(this, android.R.attr.textAppearanceLarge);
    titleView.setText("Hallo Welt!");
    layout.addView(titleView);

    setContentView(layout);

}
}

当我开始这个活动它不显示这个TextView的,但它也没有显示错误。 没有人有一个建议?

When i start this activity it does not show this TextView but it also does not show an error. Does anyone have an advice?

推荐答案

试试这个,

  LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        TextView titleView = new TextView(this);
        LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        titleView.setLayoutParams(lparams);
        titleView.setTextAppearance(this, android.R.attr.textAppearanceLarge);
        titleView.setText("Hallo Welt!");
        layout.addView(titleView);

        setContentView(layout);
阅读全文

相关推荐

最新文章