通过 addContentView 添加在 XML 中定义的视图
Adding a view defined in XML via addContentView
我正在使用 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 添加它,而不是像我上面那样以编程方式定义视图吗?
谢谢!
你实际上可以使用像
这样的东西
LayoutInflater.from(context).inflate(R.layout.your_xml_layout_file, this);
您可以使用 LayoutInflater 从 xml 加载视图:
LayoutInflater.from(context).inflate(R.layout.file, rootView, false);
can I define the view in an xml file and then add it via
addContentView?
是的,首先使用 LayoutInflater 扩充 xml 文件,其中 return 一个 View
。设置布局 LayoutParams
后将视图传递给 addContentView
LayoutInflater inflater = getLayoutInflater();
View view;
view = inflater.inflate(R.layout.xml_file_name, null);
//.. add LayoutParams to view
// call addContentView
addContentView(view,tlp);
我正在使用 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 添加它,而不是像我上面那样以编程方式定义视图吗?
谢谢!
你实际上可以使用像
这样的东西LayoutInflater.from(context).inflate(R.layout.your_xml_layout_file, this);
您可以使用 LayoutInflater 从 xml 加载视图:
LayoutInflater.from(context).inflate(R.layout.file, rootView, false);
can I define the view in an xml file and then add it via addContentView?
是的,首先使用 LayoutInflater 扩充 xml 文件,其中 return 一个 View
。设置布局 LayoutParams
addContentView
LayoutInflater inflater = getLayoutInflater();
View view;
view = inflater.inflate(R.layout.xml_file_name, null);
//.. add LayoutParams to view
// call addContentView
addContentView(view,tlp);