什么更快?以编程方式构建布局或导入XML文件?

时间:2014-10-13 19:51:27

标签: android xml android-layout

是否可以快速导入准备好的xml文件或使用代码构建布局?

我有一个前循环,添加大约140个LinearLayouts,并填充它们。 当我将布局导出为xml文件时会更快吗?

例如:

for(int i = 0; i < ChannelList.size(); i++){
    LinearLayout channel = new LinearLayout(this);
    LinearLayout.LayoutParams channelLayoutParam = new LinearLayout.LayoutParams(600, 9800);
    channel.setOrientation(LinearLayout.VERTICAL);

    TextView channelTitle = new TextView(this);
    channelTitle.setHeight(200);
    channelTitle.setWidth(600);
    channelTitle.setPadding(5, 5, 5, 5);
    channelTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
    channelTitle.setBackgroundColor(getResources().getColor(R.color.grey));
    channelTitle.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
    channelTitle.setText(ChannelList.get(i));
    channelTitle.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));

    channel.addView(channelTitle);

    LinearLayout channelContent = new LinearLayout(this);
    LinearLayout.LayoutParams channelContentLayoutParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    channelContent.setOrientation(LinearLayout.HORIZONTAL);

    channel.addView(channelContent,channelContentLayoutParam);
    channelList.addView(channel,channelLayoutParam);
}

0 个答案:

没有答案