代替xml文件,我可以从资产中定义布局,通过web。我如何定义布局文件不使用xml文件在android。res/布局。来资产/www
您可以通过编程方式定义您的布局(参见这里)了解详细信息。所以你所需要做的就是用你喜欢的任何标记定义你的布局,在运行时下载它们,并解析它们。
例如,您可以选择在简单布局中使用CSV,它包含如下内容: Button,wrap_content,wrap_content,Hello World
TextView,wrap_content,wrap_content,Hello World Again
然后像这样创建你的布局:
LinearLayout rootLayout = new LinearLayout(this);
rootLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
this.setContentView(rootLayout);
String data=getDataFromFile();
String[] controls = data.split("n");
for (String c:controls)
{
String[] d = c.split(",");
if(d[0].equals("Button"))
{
int width;
int height;
if (d[1].equals("wrap_content"))
{
width = LayoutParams.WRAP_CONTENT;
}
else if (d[1].equals("match_parent"))
{
width = LayoutParams.MATCH_PARENT;
}
//repeat for height
Button b = new Button(this);
b.setLayoutParams(new LayoutParams(width, height));
b.setText(d[3]);
rootLayout.addView(b);
}
}
但是你让自己的生活变得非常艰难。问问自己为什么你觉得你需要这样做,有没有更好的方法来实现你想要实现的目标?