android如何动态的添加布局

1、布局文件


 

    

    

    
 


   
    



2、主程序

private LinearLayout forecastLayout;
        

forecastLayout = (LinearLayout) findViewById(R.id.forecast_layout);



forecastLayout.removeAllViews();
  for (Forecast forecast : weather.forecastList) {
     View view = LayoutInflater.from(this).inflate(R.layout.forecast_item,           forecastLayout, false);
      TextView dateText = (TextView) view.findViewById(R.id.date_text);
      TextView infoText = (TextView) view.findViewById(R.id.info_text);
      TextView maxText = (TextView) view.findViewById(R.id.max_text);
      TextView minText = (TextView) view.findViewById(R.id.min_text);
      dateText.setText(forecast.date);
      infoText.setText(forecast.more.info);
      maxText.setText(forecast.temperature.max);
      minText.setText(forecast.temperature.min);
      forecastLayout.addView(view);
      }

 

你可能感兴趣的:(Android应用)