我正在使用addContentView将子视图添加到主视图中,如下所示:

    TableLayout.LayoutParams tlp = new TableLayout.LayoutParams(
            TableLayout.LayoutParams.WRAP_CONTENT,
            TableLayout.LayoutParams.WRAP_CONTENT);

    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_NAME);
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);

    addContentView(textView,tlp);


可以像在上面以编程方式定义视图一样,可以在xml文件中定义视图,然后通过addContentView将其添加吗?

谢谢!

最佳答案

我可以在xml文件中定义视图,然后通过添加它
  addContentView?


是的,首先使用LayoutInflater扩展xml文件,该文件返回View。设置布局addContentView后将View传递给LayoutParams

LayoutInflater inflater = getLayoutInflater();
View view;
view = inflater.inflate(R.layout.xml_file_name, null);
//.. add LayoutParams to view

// call addContentView
addContentView(view,tlp);

关于android - 通过addContentView添加以XML定义的 View ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41512336/

10-11 14:30